Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何正确循环表视图的单元格并将数组中的对象添加到新数组中_Ios_Objective C_Arrays_Uitableview - Fatal编程技术网

Ios 如何正确循环表视图的单元格并将数组中的对象添加到新数组中

Ios 如何正确循环表视图的单元格并将数组中的对象添加到新数组中,ios,objective-c,arrays,uitableview,Ios,Objective C,Arrays,Uitableview,好的,所以我在编程方面经验很少,所以请耐心等待。让我进一步解释一下……我有一个具有不同属性的player类。在表视图中,我对玩家的名字和姓氏进行编号。我想循环遍历每个单元格,获取数字first name和last name,并将其添加到一个新数组中。我不想只是把它们作为字符串添加。我想把它们放在病房后的纠察室里。我有一本字典,可以将每个数字与一个玩家ID进行匹配。我该怎么做?我有一个循环通过tableviewcells,就像这样 for (_tableViewCell in self.homeP

好的,所以我在编程方面经验很少,所以请耐心等待。让我进一步解释一下……我有一个具有不同属性的player类。在表视图中,我对玩家的名字和姓氏进行编号。我想循环遍历每个单元格,获取数字first name和last name,并将其添加到一个新数组中。我不想只是把它们作为字符串添加。我想把它们放在病房后的纠察室里。我有一本字典,可以将每个数字与一个玩家ID进行匹配。我该怎么做?我有一个循环通过tableviewcells,就像这样

for (_tableViewCell in self.homePlayers.visibleCells)

    {
        if (_tableViewCell.accessoryType  == UITableViewCellAccessoryCheckmark)
        {
            [_homeConfirmedPlayersArray addObject:[NSString stringWithFormat:@"%d %@ %@",_homePlayer.number,_homePlayer.firstName,_homePlayer.lastName]];
        }
    }
homePlayers是我循环使用的桌面视图。问题是它确实会遍历每个单元格,但它只从最后一个单元格获取数据,并为每个单元格添加一次新数组。我最终得到了8个对象,分别是名字和姓氏

我在单元格中为索引路径处的行设置homePlauer对象,如下所示

if ([tableView isEqual:self->_homePlayers])
    {
        static NSString *cellIdentifier = @"cell";
        //Step 1: Check to see if we can reuse cell from a row that is now off the screen.
        _tableViewCell = [_homePlayers dequeueReusableCellWithIdentifier:cellIdentifier];
        //Step 2: If no reusable cells create a new one
        if (_tableViewCell == nil)
        {
            _tableViewCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }

        //Add detail view accessory

        _tableViewCell.accessoryType = UITableViewCellAccessoryCheckmark;

        //Step 3: Set up cell text

        _homePlayer = _homePlayersArray[indexPath.row];
        _tableViewCell.textLabel.text = [NSString stringWithFormat:@"%d %@ %@",_homePlayer.number,_homePlayer.firstName,_homePlayer.lastName];


        //Step 4: Return the cell


        return _tableViewCell;
    }

如果您需要更多信息,请询问。感谢您提前给出答案

看起来你解决了很多新程序员的问题,使一切全球化_homePlayer看起来应该是一个局部变量。我假设发生的情况是,当UITableView通过调用
tableView:cellForRowAtIndexPath:
填充自身时,将生成的最后一个单元格将设置全局\u homePlayer。然后,当您调用另一个循环函数时,_homePlayer将已经被设置,并且您也永远不会在循环函数中更改它。这就是为什么你会得到同样的8个对象。以下是修复方法:

将步骤3设置为:

id homePlayer = _homePlayersArray[indexPath.row];
tableViewCell.textLabel.text = [NSString stringWithFormat:@"%d %@ %@", homePlayer.number, homePlayer.firstName, homePlayer.lastName];
您应该将“id”替换为homePlayer类型,这样编译器将帮助您自动完成。 我想您提到过它是一本NSDictionary,所以将id替换为
NSDictionary*

对于循环函数,请执行以下操作:(注释用于解释)


记住,只有在需要全局变量时才使用全局变量。

顺便说一句,您混合了很多语法。C++风格是自->变量和自变量。这两种方法都有效,只是想让你知道。看起来你已经有了一个包含所有数据的tableview(_homePlayer)数据源。因此,您不必尝试从表视图中读取它,您可以直接从数据源中读取它。Picker视图的工作方式类似,因此您可以使用与UIPickerView数据源相同的数据。我认为我确实需要HomeConfirmedLayerArray是全局的,因为我需要稍后使用它来填充我的pickerView。此外,homePlayer是一个播放器对象,而不是字典。我已经设置了一个字典,它有一个对应于玩家号码的对象ID。我需要能够在picker视图中选择名称,然后从中获取编号以查找对应的id。这是我想要做的还是我必须改变它?我以为homePlayer是一个自定义对象,我只是不知道是什么。我没有使用过pickerviews,但是对于任何视图,它都应该只显示数据而不包含数据(这应该可以防止当前的问题)。无论您如何显示数据,当用户选择某个内容时,您都应该获得一个NSIndexPath,然后使用它为数据数组编制索引。填充pickerview时,使用indexpath.row作为索引来选择数组中的项。然后,当用户选择一行时,使用返回的索引路径与数组匹配。我将添加,如果用户ID只存储在字典中(最好是在您的情况下),那么您可能需要考虑一个查找数组,该数组与索引路径匹配到DISTRORAR.KEY。另外,我刚刚查找了UIPickerView,所以使用selectedRowInComponent:而不是indexPath.row。
// create a new and empty array
// (your local array will forever fill up with repeated objects unless emptied somewhere)
NSMutableArray *homeConfirmedPlayersArray = [[NSMutableArray alloc] init];

// loop through all indexpaths for the visible cells
for (NSIndexPath *indexPath in [self.homePlayers indexPathsForVisibleRows]){
    // get the tablecell, only to check the accessory type
    UITableViewCell *cell = [self.homePlayers cellForRowAtIndexPath:indexPath];

    // I don't think this is necessary, especially if every cell has a checkmark
    if (cell.accessoryType  == UITableViewCellAccessoryCheckmark){
        // get the record from the home players array
        NSDictionary *homePlayer = _homePlayersArray[indexPath.row];

        // add to the copy
        [homeConfirmedPlayersArray addObject:[NSString stringWithFormat:@"%d %@ %@",homePlayer.number,homePlayer.firstName,homePlayer.lastName]];
    }
}