使用iPhone SDK获取选定单元格的文本

时间:2020-12-13 10:44:01

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

我想知道如何获取所选uitableviewcell的textLabel字符串值。

5 个解决方案

#1


48  

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

#2


3  

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

你可以使用[self。然后访问它的textLabel。文本属性,但通常有更好的方法。

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.

通常,你已经根据UITableViewController可以访问的模型数组填充了你的表。因此,在大多数情况下,处理这个问题的更好方法是取所选单元格的行号,并使用它来查找模型中的关联数据。

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

例如,假设您的控制器有一个伙伴对象数组,其中有一个name属性:

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:

通过运行查询或其他操作填充这个数组。然后在tableView中:cellForRowAtIndexPath:根据每个好友的名字构建一个表视图单元格:

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

现在,当用户选择一行时,只需从数组中取出相应的Buddy对象并对其进行处理。

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

#3


1  

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

#4


0  

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

如果有人偶然发现这一点,并想知道如何在swift中实现这一点,代码如下。还要记住使用可选绑定来打开可选的包装,并避免打印出“可选的”(“选中的项目标签”)。

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

#5


0  

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)



    }
}

#1


48  

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

#2


3  

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

你可以使用[self。然后访问它的textLabel。文本属性,但通常有更好的方法。

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.

通常,你已经根据UITableViewController可以访问的模型数组填充了你的表。因此,在大多数情况下,处理这个问题的更好方法是取所选单元格的行号,并使用它来查找模型中的关联数据。

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

例如,假设您的控制器有一个伙伴对象数组,其中有一个name属性:

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:

通过运行查询或其他操作填充这个数组。然后在tableView中:cellForRowAtIndexPath:根据每个好友的名字构建一个表视图单元格:

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

现在,当用户选择一行时,只需从数组中取出相应的Buddy对象并对其进行处理。

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

#3


1  

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

#4


0  

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

如果有人偶然发现这一点,并想知道如何在swift中实现这一点,代码如下。还要记住使用可选绑定来打开可选的包装,并避免打印出“可选的”(“选中的项目标签”)。

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

#5


0  

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)



    }
}