我很确定这真的很简单。但我无法完成这项工作。我有一个 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/