我正在處理數據從後端顯示到tableview的應用程序。 問題是,當表加載時,它獲取所有數據,但只顯示每行中數據庫的最後一項。假設最後一部分有3行,那麼它只顯示3表示每個部分有3行數據。即使在某些部分行中有9行,其餘行也顯示爲空白。如何調用numberOfRowsInSection和cellForRowAtIndexPath或者
我發現它通過斷點,表首先從數據庫中獲取所有數據,然後讀取cellForRowAtIndexPath,因爲在我的數據庫中,最後一個表有3行,它僅通過將其餘行保留爲空來顯示3行的數據。
這裏是我的numberOfRowsInSection的COE和的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;
}
數據以前面的方式顯示,但是這一次,相同的數據重疊在一起,因此字母由於重疊而以粗體顯示。 –
我可以看到你的CGRect在相同的屏幕座標上反覆創建控件。你能考慮使用自定義表格單元嗎?請參考:http://www.e-string.com/content/custom-uitableviewcells-interface-builder – Saran