简体   繁体   English

为UITableViewCell中的特定单元格设置颜色

[英]Setting a colour for a particular cell in UITableViewCell

I am trying to set the background colour of particular cells in UITableViewCell to purple. 我正在尝试将UITableViewCell中特定单元格的背景色设置为紫色。

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: cellForRowAtIndexPath方法中:

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. 通过为他们分配一个字符串值a ,当我设置返回日期时,我希望它会起作用。


Edit: 编辑:

In cellForRowAtIndexPath method : cellForRowAtIndexPath方法中:

- (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 . 为了更好地说明这一点,我之所以使用_qbColorArr是因为该数组包含一个空白(在viewDidLoad初始化)或a So, when I set a return date to the first cell, I've also done this [_qbColorArr replaceObjectAtIndex:0 withObject:@"a"] . 因此,当我将返回日期设置为第一个单元格时,我也已经完成了此[_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. 然后将在else if语句中对此进行检查,并将单元格的颜色分配为紫色。

You are not setting the default color in the else condition. 您没有在else条件中设置默认颜色。 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. 实际上,我不太理解您的问题...正如您的描述中所描述的,我会认为,如果cell.dateRe.text为空,则您的单元格将为白色,否则为紫色。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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