Tableview控制器不显示来自NSMutableArray源的字符串



我从iOS有一个愚蠢的问题开始。我只想展示一个用字符串填充的TableView存储在NSMutableArray中。我可以看到字符串在数组中,但由于某些原因TableView没有显示它们

基本上是这样的:

@interface Test ()
@property (weak, nonatomic) IBOutlet UITableView *contactList;
@property (strong, nonatomic) NSMutableArray *contactsArray;
@end
- (void)onContactFound:(NSString*)contact 
{
    [self.contactsArray addObject:contact];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.contactsArray count];
}
//4
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //5
    static NSString *cellIdentifier = @"SettingsCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    //6
    NSString *tweet = [self.contactsArray objectAtIndex:indexPath.row];
    //7
    [cell.textLabel setText:tweet];
    [cell.detailTextLabel setText:@"via Codigator"];
    return cell;
}

我认为问题出在最后一部分。我复制这段代码从一个示例(http://www.codigator.com/tutorials/ios-uitableview-tutorial-for-beginners-part-1/),说我应该添加一些动态属性但是我TableView没有这些属性在属性检查器基本上我没有@"SettingsCell"所以我想至少这是其中的一个问题,也许不适用此代码在我的例子中,它应该用另一种方式?

我认为您试图在没有创建单元格的情况下解列单元格。我想你只能拿回零细胞。你应该这样写:

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

也看一下API文档,它说:

dequeueReusableCellWithIdentifier:返回一个可重用的表视图单元格对象,根据其标识符定位。返回值:一个UITableViewCell对象,带有相关的标识符,如果在可重用单元队列中不存在,则为nil。

dequeueReusableCellWithIdentifier:

最新更新