I have tableview with some names on each cell , how can i get that name when select row ?
i know that i have to use delegate method
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
but how can i get that title of that row ?
thanks in advance
regards
row will be 0. Then it will be 1, then 2, then 3 and so on. You do this so that you can get the correct string from the array each time.
Use the below function and pass the index path of the selected row so that the particular cell is reloaded again. Another solution: store the selected row index and do a reload of tableview. Then in cellForRowAtIndexPath check for the selected row and change the accessory view of the cell.
add an 'indexPath` property to the custom table cell. initialize it in cellForRowAtIndexPath. move the tap handler from the view controller to the cell implementation. use the delegation pattern to notify the view controller about the tap event, passing the index path.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellText = selectedCell.textLabel.text;
}
This snippet retrieves the selected cell and stores its text
property in a NSString
.
However, I do not recommend this. It is better to keep the data and presentation layers separate. Use a backing data store (e.g. an array) and access it using the data in the passed indexPath
object (e.g. the index to use to access the array). This data store will be the same one used to populate the table.
Assuming you're using a standard UITableViewCell, you can get the cell by using
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
then access the text property view properties via:
cell.textLabel.text
cell.detailTextLabel.text
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With