如何在每次导航时管理单元格颜色样式

时间:2013-10-21 07:15:45

标签: ios uitableview tableview nstableviewcell

我有一个表格视图,其中我正在显示一些记录。导航到第二个视图,在第二个视图控制器中,我们有四个选项,如primary,secondary,tertia,quatrnary。当用户选择任何此选项时,它将导航回来,所选的单元格颜色将会改变。

所有选项都运作良好。但我坚持以下问题,

假设用户选择第一个单元格并将其设置为pri,它将向后导航并且颜色将为红色,但是当用户选择另一个单元格(supoose 5 cell)并再次设置为primary时,则单元格1将保持与红色相同。我不想要这个。用户一次只能将一条记录设置为pri / sec / ter / quaternary。

我将如何管理?尝试重新加载表数据,但没有工作?导航回来我在视图中使用下面的代码出现

if (singltong.primary == indes) {

    [tbleMasjidnames cellForRowAtIndexPath:tableSelection].backgroundColor = [UIColor colorWithRed:13.0f/255.0f  green:159.0f/255.0f blue:78.0f/255.0f alpha:1.0f];

}
else if (singltong.secondary == indes){
    [tbleMasjidnames cellForRowAtIndexPath:tableSelection].backgroundColor = [UIColor colorWithRed:255.0f/255.0f green:33.0f/255.0f blue:59.0f/255.f alpha:1.0f];
}
else if (singltong.tertiary == indes){
    [tbleMasjidnames cellForRowAtIndexPath:tableSelection].backgroundColor = [UIColor colorWithRed:23.0f/255.0f green:153.0f/255.0f blue:221.0f/255.0f alpha:1.0f];
  }
else if (singltong.quaternary == indes){
    [tbleMasjidnames cellForRowAtIndexPath:tableSelection].backgroundColor = [UIColor colorWithRed:255.0f/255.0f green:150.0/255.0 blue:23.0f/255.0f alpha:1.0f];
}

 //********************Cell for row****************
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"MasjidCell"; // Custom cell for masjid Name
MasjidListCell *cell = (MasjidListCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.backgroundColor = [UIColor greenColor];
if (cell == nil){
    NSArray *nib=[[NSBundle mainBundle] loadNibNamed:@"MasjidCell" owner:self options:nil];
    cell = nib[0];
}
cell.selectionStyle=UITableViewCellSelectionStyleNone;

//if search is on then get data from search result array else from shared instance
if (_isSearchOn) {
    if ([searchResults count]==0) {
        cell.lblMasjidName.text=@"No Record Found.";
    }
    else{
        cell.lblMasjidName.text = [searchResults objectAtIndex:indexPath.row];
    }
}
else{
    cell.lblMasjidName.text = singltong.MasjidNames[indexPath.row];
    cell.lblLocalArea.text  = singltong.masjidLocalArea[indexPath.row];
    cell.lblCountry.text    = singltong.masjidCountry[indexPath.row];
}
return cell;

} //方法结束

1 个答案:

答案 0 :(得分:1)

如果单元格绑定到数据库中的实体,则必须在优先级选择控制器中运行逻辑,在选择之后,具有此优先级的其他对象设置回或表现得像你想要的。如果您使用NSFetchedResultsController,您的数据将保留为数据,您可以使用cellForRowAtIndexpath方法进行默认处理

if ([entity.prio intValue] == 1)
{
    // set your cell settings
}

所有受影响的单元格都必须更新,因此[tableView reloadData];中的viewWillAppear可能会有所帮助


更新

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [tbleMasjidnames reloadData];
    // ...
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // ... get/create cell instance
    if (_isSearchOn) 
    {
        if ([searchResults count]==0) 
        {
            cell.lblMasjidName.text=@"No Record Found.";
        }
        else
        {
            cell.lblMasjidName.text = [searchResults objectAtIndex:indexPath.row];
        }
    }
    else
    {
        cell.lblMasjidName.text = singltong.MasjidNames[indexPath.row];
        cell.lblLocalArea.text  = singltong.masjidLocalArea[indexPath.row];
        cell.lblCountry.text    = singltong.masjidCountry[indexPath.row];

        switch(indexPath.row)
        {
            case singltong.primary:
                cell.backgroundColor = [UIColor colorWithRed:13.0f/255.0f  green:159.0f/255.0f blue:78.0f/255.0f alpha:1.0f];
                break;

            case singltong.secondary: 
                cell.backgroundColor = [[UIColor colorWithRed:255.0f/255.0f green:33.0f/255.0f blue:59.0f/255.f alpha:1.0f];
                break;
// ...
            default:
                cell.backgroundColor = [UIColor whiteColor]; // or other default
                break;
        }
    }    
}

但您仍需在singltong课程中实施正确的处理逻辑才能使其正常工作。