-1

我有一个表格视图,在单元格中有 2 个子视图(带有标签的小缩略图),如果没有要加载的内容(当只有第一个子视图具有要加载的图像和标题时),我想隐藏第二个子视图。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *reuse = @"reuse";
    ContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:[Utils buildNibNameFromPrefix:@"ContentTableViewCell"] owner:self options:nil] objectAtIndex:0];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    cell.cellIndex = indexPath.row;

    NSUInteger selectedIndex = [Utils getIndexForContentTitle:[Utils getContentBookmark]];
    NSUInteger titleIndex = indexPath.row * self.noOfContentPerCell;
    NSUInteger cellIndex = NSNotFound;
    for (int i = 0; i < self.noOfContentPerCell; i++) {
        @try {
            if (titleIndex == selectedIndex) {
                cellIndex = i;
            }

            NSArray *content;
            if ([[NSUserDefaults standardUserDefaults] boolForKey:@"unlock"] == NO) {
                content = CONTENT_INDEXS;
            }
            else {
                content = CONTENT_INDEXS_UNLOCKED;
            }

            NSString *title = [content objectAtIndex:titleIndex];
            [cell setTitle:title forContentAtIndex:i];

            NSString *thumbnail;

            if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone )
            {
                thumbnail = [NSString stringWithFormat:@"%@-iphone-thumbnail.jpg", title];
            }
            else
            {
                thumbnail = [NSString stringWithFormat:@"%@-ipad-thumbnail.jpg", title];
            }

            [cell setImageNamed:thumbnail atIndex:i];

            [cell showContainerAtIndex:i];
        }
        @catch (NSException *exception) {
            [cell hideContainerAtIndex:i];
        }
        titleIndex++;
    }

    return cell;
}

它适用于 iOS 6,如果没有要加载的内容,@catch 隐藏此子视图,但在 iOS 5 上崩溃发生在:

NSString *title = [content objectAtIndex:titleIndex];
4

1 回答 1

7

这似乎很愚蠢。
- 我通常讨厌Try/Catch,除非没有其他办法。
- 为什么不检查该集合中是否index存在。
- 换句话说,不是查看是否会有 ,而是error检查会导致的条件error并处理它。

于 2013-02-20T02:07:29.923 回答