lab12
lab12

Reputation: 6448

Get textLabel text of selected Cell using the iPhone SDK

I would like to know how to get the textLabel string value of the selected uitableviewcell.

Upvotes: 19

Views: 23868

Answers (5)

mzonerz
mzonerz

Reputation: 1250

Here what i used;very simple

    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){

    if editingStyle == UITableViewCellEditingStyle.Delete
    {

        //create cellobj with indexpath for get it text
         let cell = tableView.cellForRowAtIndexPath(indexPath)
        let celltext = (cell?.textLabel?.text!)! as String
        //do what ever you want with value
        print((cell?.textLabel?.text!)! as String)



    }
}

Upvotes: 0

user1940321
user1940321

Reputation:

If anyone stumbles across this and is wondering how to do this in swift the code is below. Also remember to use optional binding to unwrap that optional and also avoid printing out "Optional("Tapped Item Label")".

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let cell = tableView.cellForRowAtIndexPath(indexPath)

        // Unwrap that optional
        if let label = cell?.textLabel?.text {
            println("Tapped \(label)")
        }
    }

Upvotes: 0

jignesh
jignesh

Reputation: 41

if (selected)
{
    indicator.image = [UIImage imageNamed:@"IsSelected.png"];
    [arrSlectedItem addObject:strselecteditem];
    NSLog(@"-- added name is %@", strselecteditem);

}
else
{
    indicator.image = [UIImage imageNamed:@"NotSelected.png"];
    [arrSlectedItem removeObject:strselecteditem];
    NSLog(@"--- remove element is -- %@", strselecteditem);
}

Upvotes: 1

cduhn
cduhn

Reputation: 17918

You can get the cell using [self.tableView cellForRowAtIndexPath:] and then access its textLabel.text property, but there's usually a better way.

Typically you've populated your table based on some model array that your UITableViewController has access to. So a better way to handle this in most cases is to take the row number of the cell that was selected and use that to find the associated data in your model.

For example, let's say your controller has an array of Buddy objects, which have a name property:

NSArray *buddies;

You populate this array by running a query or something. Then in tableView:cellForRowAtIndexPath: you build a table view cell based on each buddy's name:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BuddyCell"];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BuddyCell"] autorelease];
    }
    cell.textLabel.text = [buddies objectAtIndex:indexPath.row];
    return cell;
}

Now when the user selects a row, you just pull the corresponding Buddy object out of your array and do something with it.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    Buddy *myBuddy = [buddies objectAtIndex:indexPath.row];
    NSLog (@"Buddy selected: %@", myBuddy.name);
}

Upvotes: 3

Thomas Clayson
Thomas Clayson

Reputation: 29925

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
   // now you can use cell.textLabel.text
}

Upvotes: 49

Related Questions