这是我的代码,它不起作用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier;
if (indexPath.row == 0)
{
// buttonCell
CellIdentifier = @"buttonCell";
buttonCell *cell = (buttonCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.startAddressLabel.text = @"something";
//cell config
//debug
NSLog(@"indexPath.row: %d", indexPath.row);
NSLog(@"Cell Identifier: %@", CellIdentifier);
return cell;
} else if (indexPath.row == 1)
{
//mutableCaptionCell date&time
CellIdentifier = @"mutableCaptionCell";
mutableCaptionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//cell config
cell.infoLabel.text = @"Время и дата";
cell.contentLabel.text = @"";
//debug
NSLog(@"indexPath.row: %d", indexPath.row);
return cell;
} else if (indexPath.row == 2)
{
//mutableCaptionCell tax
CellIdentifier = @"mutableCaptionCell";
mutableCaptionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//cell config
cell.infoLabel.text = @"Тариф";
cell.contentLabel.text = @"";
//debug
NSLog(@"indexPath.row: %d", indexPath.row);
return cell;
} else if (indexPath.row == 3)
{
//mutableCaptionCell car
CellIdentifier = @"mutableCaptionCell";
mutableCaptionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//cell config
cell.infoLabel.text = @"Выбор машины на карте";
cell.contentLabel.text = @"";
//debug
NSLog(@"indexPath.row: %d", indexPath.row);
return cell;
} else if (indexPath.row == 4)
{
//wishCell wishlist
CellIdentifier = @"wishCell";
WishCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//cell config
cell.infoLabel.text = @"Пожелания";
cell.contentLabel.text = @"";
//debug
NSLog(@"indexPath.row: %d", indexPath.row);
return cell;
} else
{
//debug
NSLog(@"indexPath.row: %d", indexPath.row);
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mutableCaptionCell" forIndexPath:indexPath];
// Configure the cell...
cell.textLabel.text = @"ERROR CELL";
//debug
//NSLog(@"indexPath.row: %d", indexPath.row);
return cell;
;
}
}
我已经说过每个原型的UITableViewCell子类,重用标识符匹配每个原型,但是我在运行时看到的是UITableView不使用这些原型。例如,第一个单元格必须是巨大的单元格,有两个按钮,但是相反,它被显示为空的。我NSLoged所有内容,并在日志中显示如下:
2014-02-18 10:13:51.587 app[1624:70b] indexPath.row: 0
2014-02-18 10:13:51.590 app[1624:70b] Cell Identifier: buttonCell
2014-02-18 10:13:51.594 app[1624:70b] cell: <buttonCell: 0x8b95e00; baseClass = UITableViewCell; frame = (0 0; 320 44); autoresize = W; layer = <CALayer: 0x8b96000>>
2014-02-18 10:13:51.600 app[1624:70b] indexPath.row: 1
2014-02-18 10:13:51.603 app[1624:70b] indexPath.row: 2
2014-02-18 10:13:51.606 app[1624:70b] indexPath.row: 3
2014-02-18 10:13:51.610 app[1624:70b] indexPath.row: 4
但是TableView仍然没有使用正确的原型。
我试图使用动态原型,因为我需要在运行时改变一些单元格的高度,也许有一种方法可以用静态单元格来实现呢?
Sidenote:我在iOS 7中使用Xcode 5
发布于 2014-02-18 07:06:27
如果单元格的数量有限,使用静态单元格是很好的。当我们滚动表视图时,单元格会一次又一次地被放置。所需时间将非常少,无论它捡到的细胞,它显示相同的高度,所有的细胞.我建议去静态的细胞或任何不同的表视图,如果可能的话.
发布于 2014-02-18 06:35:45
我的猜测是,您没有初始化您的单元格,dequeueReusableCellWithIdentifier
还不够。它重用已经创建的单元格,因此如果排队列导致nil
,那么首先应该分配nil
。
可能:
if (indexPath.row == 0)
{
// buttonCell
CellIdentifier = @"buttonCell";
buttonCell *cell = (buttonCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewStylePlain reuseIdentifier:CellIdentifier];
}
cell.startAddressLabel.text = @"something";
//cell config
//debug
NSLog(@"indexPath.row: %d", indexPath.row);
NSLog(@"Cell Identifier: %@", CellIdentifier);
return cell;
}
或使用IB设计的负载传感器:
if (indexPath.row == 0)
{
// buttonCell
CellIdentifier = @"buttonCell";
buttonCell *cell = (buttonCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if(!cell)
{
cell = [[[NSBundle mainBundle] loadNibNamed:@"MyCustomCell" owner:nil options:nil] objectAtIndex:0]; // Assign reuseIdentifier from IB
}
cell.startAddressLabel.text = @"something";
//cell config
//debug
NSLog(@"indexPath.row: %d", indexPath.row);
NSLog(@"Cell Identifier: %@", CellIdentifier);
return cell;
}
希望能帮上忙!
https://stackoverflow.com/questions/21845833
复制相似问题