UITableView reloadData方法崩溃应用程序

时间:2014-12-10 05:21:28

标签: ios objective-c uitableview

在我的视图控制器中,我有一个不断添加对象的数组。在同一视图中还有一个tableView。我想填充tableView并在数组更新时更新它。

// Defines the table
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

// Defines the table - Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.items.count;
}

 // Fill the table - Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    if (self.items && self.items.count > 0) {
        BulkItem *item = [self.items objectAtIndex:indexPath.row];
        if (item) {
            // Cell Title Text

            //            // define the range you're interested in
            //            NSRange stringRange = {0, MIN([asset.name length], 1)};
            //            // adjust the range to include dependent chars
            //            stringRange = [asset.name rangeOfComposedCharacterSequencesForRange:stringRange];
            //            // Now you can create the short string
            //            NSString *typeSubstring = [[asset.name uppercaseString] substringWithRange:stringRange];

            //    cell.textLabel.text = [NSString stringWithFormat:@"%@ - %@", typeSubstring, asset.name];
            cell.textLabel.text = [NSString stringWithString:item.bulkItemMaster.name];

        }
    }

    return cell;
}

更新阵列时,我使用:

[self.itemTableView reloadData];

更新tableView。但是,在我到达我的代码的这部分后,它崩溃了。它给了我这个错误信息:

"2014-12-09 23:07:28.156 va-std[750:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSPlaceholderString initWithString:]: nil argument'"

有关如何解决此问题的任何想法?

1 个答案:

答案 0 :(得分:0)

这条线是一场噩梦,

cell.textLabel.text = [NSString stringWithString:item.bulkItemMaster.name];

在为标签指定名称之前,请检查不是nil。

if(item.bulkItemMaster.name) {
    cell.textLabel.text = [NSString stringWithString:item.bulkItemMaster.name];
}