Question

I have one table view in which i am displaying some record. And navigte to second view, in second view controller we have four options as primary, secondary, tertia, quatrnary. when user select any of this optin it will navigate to back and selected cell color will change.

All option is working well. but i stuck at following issue as,

suppose user select first cell and set this as pri, it will navigate back and color will be red, BUT when user slected another cell (supoose 5 cell) and again set as primary then cell 1 will remaining same as red. i dont want this. at a time user will set noly one record as pri/sec/ter/quaternary.

How i will manage this?? Tried to relaod table data, but not working?? while navigating back i am using below code in viewwill appear

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;

}//end of method

Was it helpful?

Solution

If your cells are bound to Entities in a Database, you have to run a logic in the priority selection controller where after the selection, other objects with this priority are set back or act like you want. If you use the NSFetchedResultsController, your data is hold up-to-data and you can do a default handling in the cellForRowAtIndexpath method

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

all of your affected cells have to be updated, so maybe a [tableView reloadData]; in viewWillAppear will help


update:

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

but you still have to implement correct handling logic in your singltong class to make this working.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top