1

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

user2902101
  • 493
  • 1
  • 9
  • 17
  • are your cells bound to any entities or how do you handle it? can you show your `cellForRowAtIndexpath` method? – geo Oct 21 '13 at 07:43
  • Is your `singltong` class/object holding the selected cells index and the the singltong.primary etc. are set with this index? If yes, you should think of change the structure so, that you have n objects that represent the cells and every object is holding a priority. If you don't want to change for what reasons ever, I will update my answer to this structure, even I would prefere to do it an other way ;) – geo Oct 21 '13 at 08:31

1 Answers1

1

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.

geo
  • 1,781
  • 1
  • 18
  • 30
  • hey but every time when i navigate back my all cell color will remove – user2902101 Oct 21 '13 at 07:41
  • need to keep all selected roe but not of same color with your solution my all cell is removed – user2902101 Oct 21 '13 at 07:51
  • My solution will noch remove all colors, by refresh it, depending on the state of the object that is bound to this cell. Please show, how your `cellForRowAtIndexpath` is built and maybe I can specify my answer to your requirentments ;) – geo Oct 21 '13 at 07:57
  • i have added cell for row also – user2902101 Oct 21 '13 at 08:08
  • I have updated my answer. I hope I understood and used your `singltong` class correct. As told, don't like the structure, would do it an other way, but hope to give you still a valid solution to your issue – geo Oct 21 '13 at 09:16
  • hello it works like, need to add NSindex path in globally and every time just check with your previous inde... – user2902101 Oct 24 '13 at 07:22