Ios 如何在一个字符串中获取UITableView的选定单元格的值
我很确定这很简单。但我不能让这一切顺利进行。我有一个UITableView,动态显示facebook好友列表,感谢他们的FBID。基本上,我想返回我选择的朋友的FBID,在一个字符串中用逗号分隔。如果可能的话,在iAction中,我可以将它们传递到php的参数中。 例如,让我们假设我有一个4个朋友的列表,a B C D,他们的ID是1,2,3,4。 如果我选择A和C,我想要一个表示1,3的字符串 我所能做的就是显示我选择的朋友的id,一次显示一个。 这是我的密码:Ios 如何在一个字符串中获取UITableView的选定单元格的值,ios,objective-c,uitableview,nsstring,didselectrowatindexpath,Ios,Objective C,Uitableview,Nsstring,Didselectrowatindexpath,我很确定这很简单。但我不能让这一切顺利进行。我有一个UITableView,动态显示facebook好友列表,感谢他们的FBID。基本上,我想返回我选择的朋友的FBID,在一个字符串中用逗号分隔。如果可能的话,在iAction中,我可以将它们传递到php的参数中。 例如,让我们假设我有一个4个朋友的列表,a B C D,他们的ID是1,2,3,4。 如果我选择A和C,我想要一个表示1,3的字符串 我所能做的就是显示我选择的朋友的id,一次显示一个。 这是我的密码: -(void)tableVie
-(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);
}
提前感谢您,我相信这并不复杂。问题是您没有使用IndExpathForSelectedRows中的信息。这将告诉您所有选定的行。相反,您只需查看indexPath.row,它是最近选择的一行
您需要做的是在IndExpathForSelectedRows中循环,并收集当前选定的每一行的信息。我假设您在这里启用了多重选择。成功地使其工作!!!感谢一百万以下是解决方法:indexPathArray中的forNSIndexPath*索引{NSString*fbID=[idFriends objectAtIndex:index.row];listOfFacebookIDs=[listOfFacebookIDs stringByAppendingString:[NSString stringWithFormat:@%@,fbID]];}如果[listOfFacebookIDs length]>0 ListoFaceBookIDS=[ListoFaceBookIDS substringToIndex:[ListoFaceBookIDS长度]-1];NSLog@Your逗号分隔的字符串为%@,ListofFaceBookId//现在把这份清单传给你想去的任何地方
-(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
}
Use the following code to get the selected rows in a table view. :)
NSArray *selectedRows=[tableView indexPathsForSelectedRows];
NSMutableArray *rownumberArray=[[NSMutableArray alloc]init];
for (int i=0; i<selectedRows.count; i++) {
NSIndexPath *indexPath = [selectedRows objectAtIndex:i];
NSInteger row = indexPath.row;
NSNumber *number = [NSNumber numberWithInteger:row];
[rownumberArray addObject:number];
}
// rownumberArray contains the row numbers of selected cells.