ios - 每次导航返回时如何管理单元格颜色样式

标签 ios uitableview tableview nstableviewcell

我有一个表格 View ,其中显示了一些记录。然后导航到第二个 View ,在第二个 View Controller 中,我们有四个选项作为主要、次要、第三、第四。当用户选择任何此选项时,它将导航到后退并且选定的单元格颜色将更改。

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

假设用户选择第一个单元格并将其设置为 pri,它将返回并且颜色将为红色,但是当用户选择另一个单元格(假设 5 个单元格)并再次设置为主要单元格时,单元格 1 将保持为红色。我不想要这个。一次用户不会将一条记录设置为 pri/sec/ter/quaternary。

我将如何管理这个??试图重新加载表数据,但不起作用??向后导航时,我在 View 中使用以下代码将出现

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;

}//方法结束

最佳答案

如果您的单元格绑定(bind)到数据库中的实体,您必须在优先级选择 Controller 中运行逻辑,在选择之后,具有此优先级的其他对象将退回 strong> 或随心所欲。如果您使用 NSFetchedResultsController,您的数据是最新数据,您可以在 cellForRowAtIndexpath 方法中进行默认处理

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

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


更新:

- (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 类中实现正确的处理逻辑才能使其正常工作。

关于ios - 每次导航返回时如何管理单元格颜色样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19488295/

相关文章:

iphone - 我们可以先显示第二部分吗???

ios - 如何在 View 中垂直居中标签

ios - UITableView 设置背景颜色

css 不适用于表格 View 的选定行以更改文本颜色

JavaFX TableView 列适合内容

ios - UIPopover 与 UINavigationController 和 UITableView : tableView delegates not called

ios - Apple 在 ARC 文档警告中针对通过引用传递的警告是什么?

ios - 设置从 HTML 字符串生成的 UITableViewCell 标签

ios - UITableViewAutomaticDimension 未按预期工作。 swift

ios - react native : Setting flex:1 on a ScrollView contentContainerStyle causes overlapping of components