ios - 如何在一个字符串中获取 UITableView 的选定单元格的值

标签 ios objective-c uitableview nsstring didselectrowatindexpath

我很确定这真的很简单。但我无法完成这项工作。我有一个 UITableView,我在其中动态显示 facebook 好友列表,这要感谢他们的 FBID。基本上,我想在用逗号分隔的一个字符串中返回我选择的 friend 的 FBID。如果可能的话,在 IBAction 中,这样我就可以将它们传递给 php 的参数。 例如,假设我有一个包含 4 个 friend 的列表,A B C D,他们的 ID 是 1,2,3,4。 如果我选择 A 和 C,我想要一个表示 1,3 的字符串。

我所能做的就是显示我选择的 friend 的 ID,一次显示一个。 这是我的代码:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

rowcount = [[tableView indexPathsForSelectedRows] count];
indexer = [idFriends objectAtIndex:indexPath.row];
aapell = [NSString stringWithFormat:@"%@", indexer];

NSMutableString * arrayIds = [[NSMutableString alloc] init];
[arrayIds appendString:aapell];

NSLog(@"ids: %@", arrayIds);


}

提前谢谢你,我相信这并不复杂。

最佳答案

-(IBAction)gatherFBIds
{
    NSString *listOfFacebookIDs = @"";
    NSArray *indexPathArray = [self.tableView indexPathsForSelectedRows];
    for(NSIndexPath *index in indexPathArray)
    {
        //Assuming that 'idFriends' is an array you've made containing the id's (and in the same order as your tableview)
        //Otherwise embed the ID as a property of a custom tableViewCell and access directly from the cell
        //Also assuming you only have one section inside your tableview
        NSString *fbID = [idFriends objectAtIndex:index.row];
        if([indexPathArray lastObject]!=index)
        {
            listOfFacebookIDs = [listOfFacebookIDs stringByAppendingString:[fbID stringByAppendingString:@", "]];
        }
        else
        {
            listOfFacebookIDs = [listOfFacebookIDs stringByAppendingString:fbID];

        }
    }

    NSLog(@"Your comma separated string is %@",listOfFacebookIDs);
    //Now pass this list to wherever you'd like
}

关于ios - 如何在一个字符串中获取 UITableView 的选定单元格的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16492849/

相关文章:

objective-c - ScrollView 中图像之间的差距

ios - 带有/文本字段的 UIAlert 到表

ios - 如何自动滑动/滑动单元格tableviewcell swift 3

ios - 库缓存路径中 iOS 应用程序 Cache.db 的用途是什么?

ios - 如何设置 uitableview 的样式以创建漂亮的菜单

iphone - 如何使用 RestKit .20 "Fire and forget"某些请求,例如 DELETE 或 PUT 甚至某些 POST?

ios - UITableView WillDisplayCell 方法在特定时间后调用?

ios - 从 UIScrollView 中删除 subview 并更新 View

ios - 添加一个小视频剪辑作为背景

iphone - 如何从一个 View Controller 弹出到另一个 View Controller