索引路径处的行的表视图方法单元格再次重新加载,然后应用程序崩溃

发布于 2024-11-17 08:38:15 字数 1416 浏览 0 评论 0原文

在我的应用程序中,我在一行中有一个表格视图和 4 个图像视图。当我滚动表视图时,再次调用索引路径方法中的行单元格,然后应用程序崩溃。如何防止滚动时表视图重新加载。我的代码部分是:-

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = nil;
    static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier";


    cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier] autorelease];

        for (int i=0; i <= [wordsInSentence count]; ++i) {
            UIImageView *imageView1 = [[[UIImageView alloc] initWithFrame:CGRectMake(30+90*(i%4), 15, 80, 100)] autorelease] ;
            imageView1.tag = i+1;

            [imageViewArray insertObject:imageView1 atIndex:i];
            [cell.contentView addSubview:imageView1];
        }

    }

    int photosInRow;

    if ( (indexPath.row < [tableView numberOfRowsInSection:indexPath.section] - 1) || ([wordsInSentence count] % 4 == 0) ) {
        photosInRow = 4;
    } else {
        photosInRow = [wordsInSentence count] % 4;
    }

    for ( int i = 1; i <=photosInRow ; i++ ){
        imageView = (UIImageView *)[cell.contentView viewWithTag:j];
        [self showImage:imageView];
    }

    return cell;
}

请帮忙。任何帮助将不胜感激。

谢谢, 克里斯蒂

In my app I have a table view and 4 images view in one row. When I scroll the table view then cell for row at index path method is again called and then the app crashes. How can I prevent table view reloading when scrolling. My part of code is :-

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = nil;
    static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier";


    cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier] autorelease];

        for (int i=0; i <= [wordsInSentence count]; ++i) {
            UIImageView *imageView1 = [[[UIImageView alloc] initWithFrame:CGRectMake(30+90*(i%4), 15, 80, 100)] autorelease] ;
            imageView1.tag = i+1;

            [imageViewArray insertObject:imageView1 atIndex:i];
            [cell.contentView addSubview:imageView1];
        }

    }

    int photosInRow;

    if ( (indexPath.row < [tableView numberOfRowsInSection:indexPath.section] - 1) || ([wordsInSentence count] % 4 == 0) ) {
        photosInRow = 4;
    } else {
        photosInRow = [wordsInSentence count] % 4;
    }

    for ( int i = 1; i <=photosInRow ; i++ ){
        imageView = (UIImageView *)[cell.contentView viewWithTag:j];
        [self showImage:imageView];
    }

    return cell;
}

Please help. Any help will be appreciated.

Thanks,
Christy

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

智商已欠费 2024-11-24 08:38:15

我看到的唯一问题是,

for ( int i = 1; i <= photosInRow ; i++ ){
    imageView = (UIImageView *)[cell.contentView viewWithTag:j];
    [self showImage:imageView];
}

这里的 j 是什么?我建议您将其更改为 i 即,

for ( int i = 1; i <=photosInRow ; i++ ){
    imageView = (UIImageView *)[cell.contentView viewWithTag:i];
    [self showImage:imageView];
}

因为我看到的唯一其他缺陷就是这里的逻辑,

for (int i=0; i <= [wordsInSentence count]; ++i) {
    UIImageView *imageView1 = [[[UIImageView alloc] initWithFrame:CGRectMake(30+90*(i%4), 15, 80, 100)] autorelease] ;
    imageView1.tag = i+1;

    [imageViewArray insertObject:imageView1 atIndex:i];
    [cell.contentView addSubview:imageView1];
}

您只需要连续添加 4 个图像视图。不是每行中图像视图的总数。这是有缺陷的逻辑。建议更新为

for (int i = 0; i < 4; ++i) {
    UIImageView *imageView1 = [[[UIImageView alloc] initWithFrame:CGRectMake(30+90*(i%4), 15, 80, 100)] autorelease] ;
    imageView1.tag = i+1;

    int trueImageIndex = indexPath.row * 4 + i;
    [imageViewArray insertObject:imageView1 atIndex:trueImageIndex];

    [cell.contentView addSubview:imageView1];
}

The only problem I see is this,

for ( int i = 1; i <= photosInRow ; i++ ){
    imageView = (UIImageView *)[cell.contentView viewWithTag:j];
    [self showImage:imageView];
}

What is j here? I suggest you change that to i i.e.

for ( int i = 1; i <=photosInRow ; i++ ){
    imageView = (UIImageView *)[cell.contentView viewWithTag:i];
    [self showImage:imageView];
}

As such only other flaw I see is the logic here,

for (int i=0; i <= [wordsInSentence count]; ++i) {
    UIImageView *imageView1 = [[[UIImageView alloc] initWithFrame:CGRectMake(30+90*(i%4), 15, 80, 100)] autorelease] ;
    imageView1.tag = i+1;

    [imageViewArray insertObject:imageView1 atIndex:i];
    [cell.contentView addSubview:imageView1];
}

You only need to add 4 image views in a row. Not the the total number of image views in each row. This is flawed logic. Suggested update to

for (int i = 0; i < 4; ++i) {
    UIImageView *imageView1 = [[[UIImageView alloc] initWithFrame:CGRectMake(30+90*(i%4), 15, 80, 100)] autorelease] ;
    imageView1.tag = i+1;

    int trueImageIndex = indexPath.row * 4 + i;
    [imageViewArray insertObject:imageView1 atIndex:trueImageIndex];

    [cell.contentView addSubview:imageView1];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文