[英]UITableView with Custom cell
我有一個UIView,並在上面放置了一個表格視圖,該表格視圖使用來自NSArray的數據提供的自定義單元格。 發生一些奇怪的事情是,即使那里只有10個單元格並且所有10個單元格都被填充,我最多只能使表格顯示2個單元格。
我有一個不同的筆尖,以相同的方式排列,效果很好。
正在發生的事情的屏幕截圖(5是所有單元格中填充數據的10個單元格中的第五個)。
http://img692.imageshack.us/img692/1465/screenshot20100708at215.jpg
-(UITableViewCell *)tableView:(UITableView *)tblView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"ISB points cellForRowAtIndexPath start");
NSLog(@"Elements in array = %d",[self.listData count]);
//
static NSString *cellID = @"PointsCellIdentifier";
PointsCustomCell *cell = [tblView dequeueReusableCellWithIdentifier:cellID];
if( cell == nil )
{
NSArray *nibObjects = [[NSBundle mainBundle]loadNibNamed:@"PointsCustomCell" owner:nil options:nil];
for( id currentObject in nibObjects )
{
NSLog(@"nibObjects count = %d", [nibObjects count]);
if( [currentObject isKindOfClass:[PointsCustomCell class]] )
{
cell = (PointsCustomCell *)currentObject;
}// if
}// for
}// if
if( indexPath.row < [listData count] )
{
RiderPointsData *riderPts = (RiderPointsData *)[listData objectAtIndex:indexPath.row];
cell.posLabel.text = riderPts.pos;
cell.nameLabel.text = riderPts.name;
cell.pointsLabel.text = riderPts.points;
cell.winsLabel.text = riderPts.wins;
//[riderPts release];
}
NSLog(@"ISB points cellForRowAtIndexPath end");
return cell;
}
看來您正在遍歷筆尖中的所有對象,其中可能包括UITableViewCell和它包含的4個UILabel,並且只有部分時間可以正確分配單元格。
您不需要nibObjects
數組或for循環。 如果給loadNibNamed
一個owner
參數,它將把文件的所有者設置為該值。 您也不需要indexPath.row < [listData count]
檢查您的numberOfRowsInSection方法是否返回[listData count]
。 因此,將您的代碼更改為:
-(UITableViewCell *)tableView:(UITableView *)tblView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = @"PointsCellIdentifier";
PointsCustomCell *cell = [tblView dequeueReusableCellWithIdentifier:cellID];
if( cell == nil )
{
[[NSBundle mainBundle]loadNibNamed:@"PointsCustomCell" owner:self options:nil];
// assign IB outlet to cell
cell = self.cellOutlet; // cellOutlet should be whatever you name your IBOutlet
self.cellOutlet = nil;
}// if
RiderPointsData *riderPts = (RiderPointsData *)[listData objectAtIndex:indexPath.row];
cell.posLabel.text = riderPts.pos;
cell.nameLabel.text = riderPts.name;
cell.pointsLabel.text = riderPts.points;
cell.winsLabel.text = riderPts.wins;
return cell;
}
和:
看起來listData中的某些數據為NULL。 您如何填充listData? 我想如果您添加這兩行並查看控制台輸出會更好。
//add this line in tableView:cellForRowAtIndexPath:
NSLog(@"indexPath.row = %d listData.count = %d ",indexPath.row,[listData count]);
//add this line in if( indexPath.row < [listData count] )
NSLog(@"RiderPointsData = %@ %@", riderPts,[riderPts class]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.