简体   繁体   中英

Setting a colour for a particular cell in UITableViewCell

I am trying to set the background colour of particular cells in UITableViewCell to purple.

So far, I've tried this and it sets the colour of all the cells to purple instead of only the first cell.

In cellForRowAtIndexPath method:

for(int k = 0; k < queueMCDate.count; k++){
    if(_qbColorArr.count == 0) {

    }
    else if ([[_qbColorArr objectAtIndex:k]  isEqual:@"a"]) {
        if(indexPath.row == k)
        cell.backgroundColor = [UIColor colorWithRed:0.48 green:0.04 blue:0.41 alpha:1.0];
    }
}

return cell;
}

Here's a link showing how it currently looks right now.

链接

How I would want the app to work is, after setting the return dates in the first cell, I would want it to be purple. By, assigning a string value of a to them, when I set the return dates, I was hoping that it would work.


Edit:

In cellForRowAtIndexPath method :

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

cell.queueNo.text = fQueueNo[indexPath.row];
cell.queueeName.text = queueNameDisp[indexPath.row];
cell.bkTitle.text = queueBk[indexPath.row];
cell.dateRe.text = queueRDate[indexPath.row];
cell.dateBo.text = queueBDate[indexPath.row];

for(int k = 0; k < queueRDate.count; k++){
    if(_qbColorArr.count == 0) {
         cell.backgroundColor = [UIColor whiteColor];
         return cell;
    }
    else if ([[_qbColorArr objectAtIndex:k]  isEqual:@"a"]) {
        if(indexPath.row == k)
            cell.backgroundColor = [UIColor colorWithRed:0.48 green:0.04 blue:0.41 alpha:1.0];
            return cell;
        else{
            cell.backgroundColor = [UIColor whiteColor];
            return cell;
        }
    }
}


}

The cells would be at default white at the start, however when a return date is added to a cell, that cell would need to turn purple, indicating that a book has been returned, whilst the others stayed white.

To better clarify, the reason I used _qbColorArr was because that the array contains either a blank (initialized at viewDidLoad ) or a . So, when I set a return date to the first cell, I've also done this [_qbColorArr replaceObjectAtIndex:0 withObject:@"a"] . This would then would be checked at the else if statement and assign the color of the cell to purple.

You are not setting the default color in the else condition. Once the color of row is set to purple, then because of reuse-identifier the color remains unchanged. Set the default color as follows:-

for(int k = 0; k < queueMCDate.count; k++){
    if(_qbColorArr.count == 0) {
       cell.backgroundColor = [UIColor whiteColor];//Your Default Color will go here 
    }
    else if ([[_qbColorArr objectAtIndex:k]  isEqual:@"a"]) {
        if(indexPath.row == k)
            cell.backgroundColor = [UIColor colorWithRed:0.48 green:0.04 blue:0.41 alpha:1.0];
        else{
            cell.backgroundColor = [UIColor whiteColor];//Your Default Color will go here
        }
    }
}

return cell;

Actually, I don't really understand your question... As what in your description, I will think if cell.dateRe.text is nothing then your cell will be white color and purple color otherwise.

So just give a try.

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

cell.queueNo.text = fQueueNo[indexPath.row];
cell.queueeName.text = queueNameDisp[indexPath.row];
cell.bkTitle.text = queueBk[indexPath.row];
cell.dateRe.text = queueRDate[indexPath.row];
cell.dateBo.text = queueBDate[indexPath.row];

if (cell.dateRe.text.length > 0){
    cell.backgroundColor = [UIColor colorWithRed:0.48 green:0.04 blue:0.41 alpha:1.0];
} else{
    cell.backgroundColor = [UIColor whiteColor];
}
return cell;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM