【问题标题】:reloadData doesn't work重载数据不起作用
【发布时间】:2012-04-29 03:41:53
【问题描述】:

我正在尝试更新我的 UITableview,但是当我调用 [tableView reloadData]; 时,表格不会更新,在我滚动单元格名称上方的表格视图同时被更改后。但它不是添加新行或类似的东西。

-(void)update {
        [tableView reloadData];

    NSLog(@" %i", [tableData count]);
}

当我添加一行返回 2 但表没有更新时,它返回 1 的 nslog。

- (void) refresh:(id)sender {  


    NSString *jsonString = [NSString 
                            stringWithContentsOfURL:[NSURL URLWithString:xmlDataUrl] 
                            encoding:NSUTF8StringEncoding
                            error:nil];


    SBJSON *parser = [[SBJSON alloc] init];
    NSDictionary *results = [parser objectWithString:jsonString error:nil];

    parser = nil;

    [self setTableData:[results objectForKey:@"items"]];
        [self performSelector:@selector(update) withObject:nil afterDelay:.2];


}

.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";

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


    NSDictionary *item = [tableData objectAtIndex:[indexPath row]];
    [[cell textLabel] setText:[item objectForKey:@"title"]];
    [[cell detailTextLabel] setText:[item objectForKey:@"descript"]];
    [[cell detailTextLabel] setText:[item objectForKey:@"detail"]];

.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

        return [tableData count];
}

【问题讨论】:

  • [self setTableData:] 是做什么的,cellForRowAtIndexPath: 方法是什么样的?
  • [self setTableData:] 在cellforrowathindexpath中设置数据(detailtext, image, title)
  • @Eimantas 我编辑了消息!与 cellForRowAtIndexPath:
  • 几件事情要检查。 IB 中的原型单元标识符是否名为“Cell”? UITableView 委托和数据源是否设置为您上面的类?
  • UItableview 在视图控制器中,并且 uitable 与委托 en 数据源(文件所有者)@Rob 连接

标签: objective-c xcode uitableview ios5


【解决方案1】:

每次我看到这个问题都是UITableView 插座没有连接的结果。因此,reloadData 呼叫被发送到nil。在这种情况下,这几乎可以肯定是错误的。这可以通过简单的日志语句轻松测试。

-(void)update {
    NSLog(@"tableView is '%@'",tableView);
    [tableView reloadData];
    NSLog(@" %i", [tableData count]);
}

您很可能会在控制台中看到tableView is (null)

旁注: 表格视图完全填充的事实表明dataSourcedelegate 出口已连接。

【讨论】:

  • 已修复,哈哈,我忘了连接 iboutlet @property (nonatomic,retain) IBOutlet UITableView *tableView;
  • right :-) tableview 不一定是 nil,但您可以测试它是否与 self.tableView = tableView 连接,例如cellForRowAtIndexPath
猜你喜欢
  • 1970-01-01
  • 2018-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多