Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get title of of row selected in UITableview

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

like image 592
appleDE Avatar asked Dec 14 '10 21:12

appleDE


People also ask

What does IndexPath row return?

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.

How do I get a cell in Didselectrowat?

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.

How do I get IndexPath from cell Swift?

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.


2 Answers

-(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.

like image 159
Evan Mulawski Avatar answered Oct 14 '22 21:10

Evan Mulawski


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
like image 22
Rog Avatar answered Oct 14 '22 20:10

Rog