gpt4 book ai didi

ios - 无法将自定义添加到我的 TableView 中的特定单元格?

转载 作者:行者123 更新时间:2023-11-29 03:29:52 25 4
gpt4 key购买 nike

我有一个 TableView ,它由数组 _stations 的内容填充。

我总共有超过 50 个电台(通过 XML 自动获取),每个电台当然都有自己的单元格行,并带有其名称标签。我现在遇到的问题是,我已手动将另一个项目添加到我的 _stations 中,因此我希望这个特定项目能够从其他项目中脱颖而出,理想情况下我希望它看起来像有点不同,背景上可能是半透明的 0.5 alpha greenColor,其他一切都一样。

为此,我尝试实现以下代码:

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
StationCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

if (_stations.count == 1) {
// THIS BEING THE SPECIFIC CELL that I have manually added.
cell.backgroundColor = [UIColor greenColor];
}

else {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.backgroundColor = [UIColor clearColor];
Station* station = [_stations objectAtIndex:indexPath.row];

delegate* appDelegate = (delegate*)[UIApplication sharedApplication].delegate;
if([station.streamURL isEqualToString:[((AVURLAsset*)(appDelegate.mainPlayer.currentItem.asset)).URL absoluteString]])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

cell.titleLabel.text = station.name;
cell.bitrateLabel.text = [NSString stringWithFormat:@"%@ kbps", station.bitRate];
}
return cell;


}

但是当我运行应用程序时,特定单元格上没有任何自定义...或任何单元格,但如果我将 clearColor 替换为 greenColor 然后它将我的所有单元格更改为 greenColor (可以理解)...

如何自定义位于数组中位置 1 的特定单元格?

最佳答案

我认为您使用的 if 条件在您的情况下不正确。

if (_stations.count == 1) {
// THIS BEING THE SPECIFIC CELL that I have manually added.
cell.backgroundColor = [UIColor greenColor];
}

_stations.count 正如您在问题中提到的那样,将始终为 50 或更多。所以这个条件永远不会满足。

要修改表格的第一个单元格,您需要使用表格 View 的索引路径。尝试用这个替换你的 if 条件。

if (indexpath.row == 0) {
// THIS BEING THE SPECIFIC CELL that I have manually added.
cell.backgroundColor = [UIColor greenColor];
}

indexpath.row 为您提供该 tableView 中单元格的索引。如果它是第一个单元格,则只需修改它。

关于ios - 无法将自定义添加到我的 TableView 中的特定单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19948492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com