reloadData不起作用
问题描述:
我试图更新我的UITableview,但是当我打电话给[tableView reloadData];
时,表格并没有更新,之后我将tableview在单元格名称上方滚动,同时被更改。但它不是增加一个新的行或类似的东西。reloadData不起作用
-(void)update {
[tableView reloadData];
NSLog(@" %i", [tableData count]);
}
当我添加一行它返回2,但表不更新。
- (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];
}
答
每次我看到这个问题,这是UITableView
插座没有连接的结果。因此,reloadData
呼叫发送到nil
。这在这种情况下几乎肯定是错误的。这可以通过简单的日志语句轻松测试。
-(void)update {
NSLog(@"tableView is '%@'",tableView);
[tableView reloadData];
NSLog(@" %i", [tableData count]);
}
更可能在控制台中看到tableView is (null)
。
旁注: 的实现代码如下填充在所有的事实是,dataSource
和delegate
出口连接的标志。
'[self setTableData:]'做了什么,'cellForRowAtIndexPath:'方法是怎么样的? – Eimantas 2012-04-17 19:06:16
[self setTableData:]正在设置cellforrowathindexpath中的数据(detailtext,image,title) – Jones 2012-04-17 19:14:15
@Eimantas我编辑了消息!与cellForRowAtIndexPath: – Jones 2012-04-17 19:15:55