无法在表格视图中向特定单元格添加自定义?

时间:2013-11-13 08:06:48

标签: ios uitableview

我有一个由我的数组_stations的内容填充的表视图。

总共我有超过50个站点(通过XML自动获取),每个站点都有自己的单元格行,并带有名称标签。我现在遇到的问题是,我已经手动向我的_stations添加了另一个项目,所以这个特别的项目我想从其他项目中脱颖而出,理想情况下我希望它看起来有点不同,也许是背景上半透明的.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中自定义位于数组中的特定单元格?

2 个答案:

答案 0 :(得分:1)

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

if (_stations.count == 1) {
    // THIS BEING THE SPECIFIC CELL that I have manually added.
    cell.backgroundColor = [UIColor greenColor];
    }
如您在问题中所提到的,

_stations.count将始终为50或更多。所以这种情况永远不会得到满足。

要修改表格的第一个单元格,您需要使用tableview的indexpath。 尝试用此替换你的if条件。

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

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

答案 1 :(得分:0)

试试这个。 。 。

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

    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
        reuseIdentifier:@"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;
相关问题