从NSIndex访问字符串

时间:2012-04-09 02:51:31

标签: iphone

我目前有一个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).

}

任何帮助?

3 个答案:

答案 0 :(得分: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]];
}

答案 1 :(得分: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];

}

答案 2 :(得分:0)

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

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

}