1

我目前有一个 UITableView,它由一个充满练习的 .plist 填充。我想要做的是通过将每个单击的练习存储到一个数组中来访问表格中的各个练习,该数组稍后将用于填充单独的 UITableView。

我究竟如何访问这些单独的单元格,以便我可以将它们存储到这个数组中。这是我到目前为止所拥有的:

-(IBAction) saveWorkout {
    NSMutableArray *workout = [NSMutableArray arrayWithCapacity:10];

    [workout addObject: ] // I'm assuming this is where I add a cell to an array (or atleast the cell's string).

}

有什么帮助吗?

4

3 回答 3

0

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

您可以通过 indexPath.row 在这里获取索引

}

于 2012-04-09T07:21:59.717 回答
0

在不深入研究问题的实际代码部分的情况下,调用-cellForRowAtIndexPath检索标题(可能)非常昂贵,尤其是在多次调用的情况下。用于-didSelectRowAtIndexPath:获取数据源数组中标题的索引,然后将该对象添加到列表中。-saveWorkout完成/达到一定限制时调用。

同样可能看起来像:

-(void)didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
     //other code and such... 
     //get the index of the object in our original array and add the corresponding object to the new array.
     [customWorkoutArray addObject:[workoutArray objectAtIndex:indexPath.row]];
}
于 2012-04-09T03:49:07.907 回答
0

在代码中重述@CodaFi:

@property (strong, nonatomic) NSMutableArray *selectedElements;
@synthesize selectedElements=_selectedElements;

- (NSMutableArray *)selectedElements {
    if (!_selectedElements) {
        _selectedElements = [NSMutableArray array];
    }
    return _selectedElements;
}

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

    id element = [self.myModel objectAtIndex:indexPath.row];
    // this is the key:  this array will now be the basis for your subsequent table of selected items
    [self.selectedElements addObject:element];

    // do something to your model here that indicates it's been selected
    // have your cellForRow... method interrogate this key and draw something special
    [element setValue:[NSNumber numberWithInt:1] forKey:@"selected"];

    // reload that row so it will look different
    [tableView beginUpdates];
    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath withRowAnimation:UITableViewRowAnimationFade];
    [tableView endUpdates];

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}
于 2012-04-09T04:10:20.680 回答