【发布时间】:2011-08-16 10:08:25
【问题描述】:
我正在开发一个数据从后端显示到表格视图的应用程序。 问题是,当表加载时,它获取所有数据,但每行只显示数据库的最后一个条目。假设最后一节有 3 行,那么它在每个节中只显示 3 行数据。即使在某些节中的行是 9 行,其余行也显示为空白。
我通过断点发现表首先从数据库中获取所有数据然后读取cellForRowAtIndexPath,因为在我的数据库中最后一个表有3行,它仅通过将其余行保持空白来显示3行数据。
这是我的 numberOfRowsInSection 和 cellForRowAtIndexPath 的 coe
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if(requestType == 2)
{
if (self.uniqueCityCount > 0)
{
NSString *strCity = [self.arrayCityNames objectAtIndex:section]; //@"EventData" :@"EventCity"
NSPredicate *predicateSettings = [NSPredicate predicateWithFormat:@"(EventCity = %@ )",strCity];
if ([self.arrayEventDescription count]>0)
{
[self.arrayEventDescription removeAllObjects];
}
self.arrayEventDescription = [CoreDataAPIMethods searchObjectsInContext:@"EventData" :predicateSettings :@"EventCity" :YES :self.managedObjectContext];
return [self.arrayEventDescription count];
}
/*
if (section == 0)
{
NSString *strCity = [self.arrayCityNames objectAtIndex:section]; //@"EventData" :@"EventCity"
NSPredicate *predicateSettings = [NSPredicate predicateWithFormat:@"(EventCity = %@ )",strCity];
self.arrayEventDescription = [CoreDataAPIMethods searchObjectsInContext:@"EventData" :predicateSettings :@"EventCity" :YES :self.managedObjectContext];
return [self.arrayEventDescription count];
}
else if (section == 1)
{
}*/
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [@"" stringByAppendingFormat:@"Cell%d",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.backgroundColor = [UIColor clearColor];
if(requestType == 2)
{
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
// for (int j = 0 ; j < [self.arrayEventDescription count]; j++)
// for(int j=0; j<[tableView numberOfSections]; j++)
//{
NSLog(@"at section %d",indexPath.section);
NSLog(@"at row %d",indexPath.row);
NSLog(@"array count %d",[self.arrayEventDescription count]);
if (indexPath.row < [self.arrayEventDescription count])
{
EventData *data = [self.arrayEventDescription objectAtIndex:indexPath.row];
//EventData *data = [self.arrayEventDescription objectAtIndex:j];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.backgroundColor=[UIColor clearColor];
// UIView *viewDescription = [[UIView alloc]initWithFrame:CGRectMake(00, 00, 480, 35)];
////////////////////// Labels for description of city events from database ////////////////////////////
UILabel *lblEvent = [[UILabel alloc]initWithFrame:CGRectMake(15, 00, 150, 30)];
lblEvent.font = [UIFont systemFontOfSize:12];
lblEvent.backgroundColor = [UIColor clearColor];
UILabel *lblEventAtDate = [[UILabel alloc]initWithFrame:CGRectMake(200, 00, 150, 30)];
lblEventAtDate.font = [UIFont systemFontOfSize:12];
lblEventAtDate.backgroundColor = [UIColor clearColor];
UILabel *lblEventAtSchool = [[UILabel alloc]initWithFrame:CGRectMake(350, 15, 150, 30)];
lblEventAtSchool.font = [UIFont systemFontOfSize:12];
lblEventAtSchool.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:lblEvent];
[cell.contentView addSubview:lblEventAtDate];
[cell.contentView addSubview:lblEventAtSchool];
lblEvent.text = data.EventName;
// lblEventAtDate.text = data.EventDate;
lblEventAtSchool.text = data.EventPlace;
}
//}
}
}
// Configure the cell...
return cell;
}
【问题讨论】:
标签: objective-c uitableview ios4 tableview