【问题标题】:How to get the values of selected cells of a UITableView in one string如何在一个字符串中获取 UITableView 的选定单元格的值
【发布时间】:2013-05-11 01:21:57
【问题描述】:

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

我所做的只是显示我选择的朋友的 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);


}

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

【问题讨论】:

    标签: ios objective-c uitableview nsstring didselectrowatindexpath


    【解决方案1】:
    -(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
    }
    

    【讨论】:

    • 设法使它工作!感谢一百万这是解决方法: for(NSIndexPath *index in indexPathArray) { NSString *fbID = [idFriends objectAtIndex:index.row]; listOfFacebookIDs = [listOfFacebookIDs stringByAppendingString:[NSString stringWithFormat:@"%@,",fbID]]; } if ( [listOfFacebookIDs 长度] > 0) listOfFacebookIDs = [listOfFacebookIDs substringToIndex:[listOfFacebookIDs 长度] - 1]; NSLog(@"你的逗号分隔字符串是 %@",listOfFacebookIDs); //现在将此列表传递到您想要的任何地方
    【解决方案2】:

    问题是您没有使用indexPathsForSelectedRows 中的信息。这就是告诉您 all 选定的行。相反,您只是查看indexPath.row,这是最近最近选择的一行

    您需要做的是循环浏览indexPathsForSelectedRows 并收集当前选择的每一行的信息(我假设您已在此处启用多项选择)。

    【讨论】:

      【解决方案3】:
          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.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-08
        • 1970-01-01
        • 2011-04-27
        • 1970-01-01
        • 2012-07-24
        • 1970-01-01
        • 2013-04-28
        • 1970-01-01
        相关资源
        最近更新 更多