简体   繁体   中英

how to get the index of a sectioned cell of a UITableView

I would like to get the index number of a selected cell from a sectioned tableview. I give the user the option to switch tableview from standard to sectioned. when it's in standard view I get the selected index with this line

NSString *myIndex = [NSString stringWithFormat:@"%d", inedxPath.row];

How would I get that same string in sectioned TableView ???

I have been toying with

NSString *myIndex = [NSString stringWithFormat:@"%d", indexPath.Section inedxPath.row];

...Hope that makes sense..Thx...??????

context: I store the index number in a dictionary and when the user comes back to the tableview the app should remind them what they selected prior by changing the Product Name text color to gray in that selected cell...I do this by saving all the index numbers of selected products and when they come back change the text color of only the index's in my list. hope that helps. mySectionedTable

and thanks again

I would store the indexPath object itself in an array, not a dictionary. Then in your tableView:cellForRowAtIndexPath: method use an if clause to test whether your array contains indexPath, and if so, color that cell's text gray.

After Edit:

If you create a mutable array called indexPathArray, then in your didSelectRowAtIndexPath: method, add a line to put the indexPath in the array:

[self.indexPathArray addObject:indexPath];

In the tableView:cellForRowAtIndexPath: method, add this code to color the selected cells gray:

if ([self.indexPathArray containsObject:indexPath]) 
        cell.textLabel.textColor = [UIColor grayColor];

You would put this code in above where you set the textLabel's text value. Depending on what else is going on in your app, you probably should add a [self.tableView reloadData] line to the viewDidAppear method, so the table view will be updated and show your gray text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM