2012-04-09 71 views
1

我目前有一个UITableView,它由一个完整的练习.plist填充。我希望能够做到的是通过将每个单击的练习存储到数组中来访问表中的单个练习,稍后将用它来填充单独的UITableView。从NSIndex访问字符串

我到底能够访问这些单个单元格,以便将它们存储到此数组中。这是我到目前为止:

-(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). 

} 

任何帮助吗?

+0

这不是直接回答你的问题,请不要把这当成苛刻的批评,但我强烈建议阅读MCV设计模式。 Apple在开发者网站上有一些指南。一旦你理解了MVC,你会发现你正在从错误的方向接近问题。模型应该驱动桌面视图,而不是其他方式。 – sosborn 2012-04-09 03:00:48

+0

我们在这里**帮助你解决你的问题,而不是为了解决它们(我们大多数人都为此付出了很多钱))。 – EmilioPelaez 2012-04-09 03:05:08

回答

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]]; 
} 
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]; 

} 
0

- (空)didSelectRowAtIndexPath方法:(NSIndexPath *)indexPath {

你可以得到indexPath.row

}