1

我有一个表格视图,其中显示了一些记录。并导航到第二个视图,在第二个视图控制器中,我们有四个选项,分别是初级、次级、第三级、第四级。当用户选择此选项中的任何一个时,它将导航到后面,并且选定的单元格颜色将改变。

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

假设用户选择第一个单元格并将其设置为 pri,它将向后导航并且颜色将为红色,但是当用户选择另一个单元格(假设 5 个单元格)并再次设置为主要单元格时,单元格 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;

}//方法结束

4

1 回答 1

1

如果您的单元格绑定到 Entities in a Database,您必须在优先级选择控制器中运行一个逻辑,在选择之后,具有此优先级的其他对象将被退回或按照您的意愿行事。如果您使用NSFetchedResultsController,您的数据将保留数据,您可以在cellForRowAtIndexpath方法中进行默认处理

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

所有受影响的单元格都必须更新,所以也许一个[tableView reloadData];inviewWillAppear会有所帮助


更新:

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

于 2013-10-21T07:26:42.650 回答