开发者

Get textLabel text of selected Cell using the iPhone SDK

I would li开发者_StackOverflow中文版ke to know how to get the textLabel string value of the selected uitableviewcell.


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


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);
}


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);
}


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)")
        }
    }


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)



    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜