vzm
vzm

Reputation: 2450

Unable to add customization to a specific cell in my table view?

I have a table view that is being populated by the contents of my array _stations.

In total I have over 50 stations (that are automatically fetched via XML), each of course gets its own cell row with a label for its name. The problem I am running into now, is that I have manually added another item to my _stations, so this particular item I would like to stand out from the rest, Ideally I'd like for it to look a little different, perhaps a semi transparent .5 alpha greenColor on the background, and everything else the same.

To do so, I have tried to implement the following code:

- (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;


}

But when I run the app, none of the customization is on the specific cell... Or any cell at all for that matter, but if I replace clearColor with greenColor then it changes all of my cells to greenColor (understandably so)...

How can I customize this specific cell that is sitting in my array in position 1?

Upvotes: 0

Views: 90

Answers (2)

Shaik Riyaz
Shaik Riyaz

Reputation: 11462

try this . . .

(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;

Upvotes: 0

udit gupta
udit gupta

Reputation: 796

I don't think the if condition you have used is correct in your case.

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

_stations.count will always be 50 or more as you have mentioned in your question. So this condition will never be met.

To modify the first cell of your table you need to use indexpath of the tableview. Try replacing your if condition with this.

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

indexpath.row gives you the index of the cell in that tableView. If its the the first cell then just modify it.

Upvotes: 1

Related Questions