我正在开发一个从后端到tableview显示数据的应用程序。 问题是,当表加载时,它获取所有数据,但只显示每行中数据库的最后一个条目。假设最后一个部分有3行,那么每个部分只显示3个3行数据。即使某些部分的行是9,那么其余部分显示为空白。
我通过断点找到它,表首先从数据库获取所有数据,然后读取cellForRowAtIndexPath,因为在我的数据库中,最后一个表有3行,它只显示3行的数据,只剩下其余的行。
这是我的numberOfRowsInSection和cellForRowAtIndexPath的系数
- (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;
}
答案 0 :(得分:0)
首先,我将所有内容都移出了cell == nil块,因为重用可能不会发生。看看这是否能解决您的问题。
- (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;
}
答案 1 :(得分:0)
您能告诉我什么是请求类型以及何时调用它(一组可能的值)。
快速解决方案:您正在检查请求类型的条件if(requestType == 2) {
,然后您正在加载单元格。在加载tableview时将调用此委托方法,但requesttype为2.因此对于indexpath.row = 0&amp; 1,它不会进入内部加载单元格,它只加载requesttype = 2。所以它正在加载第三行。