2012-04-09 87 views
0

我目前正試圖在我的iPhone應用上實現一個表視圖,該視圖從一個表視圖/類獲取數組,並使用此數組在另一個單獨視圖中填充表。下面是該方法我使用,如果它被點擊/輕敲,增加了一個練習的數組:使用數組填充表視圖的正確方法?

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

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    NSString *str = cell.textLabel.text; 
    NSUInteger *index = 0; 

    NSMutableArray *array = [[NSMutableArray alloc] init]; 
    [array insertObject:str atIndex:index]; 

    self.workout = array; 

    [array release]; 
} 

一旦保存按鈕被按下時,該陣列將被存儲在鍛鍊(陣列)的陣列,我想以另一種觀點填充。我採取了正確的方法嗎?

這裏是我的cellForRowAtIndexPath方法:

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSUInteger section = [indexPath section]; 
    NSUInteger row = [indexPath row]; 

    NSString *key = [keys objectAtIndex:section]; 
    NSArray *nameSection = [names objectForKey:key]; 

    static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: 
          SectionsTableIdentifier ]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier: SectionsTableIdentifier ] autorelease]; 
    } 

    cell.textLabel.text = [nameSection objectAtIndex:row]; 
    return cell; 
} 
+0

你可以分享這個tableview的'cellForRowAtIndexPath'方法的代碼嗎? – Shameem 2012-04-09 17:18:24

回答

1
你可能不希望用一個新的數組,每次重新初始化 self.workout

-(void) viewDidLoad 
{ 
    self.workout = [[NSMutableArray alloc] init]; 
} 

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

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    NSString *str = cell.textLabel.text; 
    [self.workout insertObject:str atIndex:0]; 
} 
+0

我能否實現將鍛鍊的當前實例存儲到另一個陣列中的單獨保存鍛鍊方法,然後清空陣列? – TopChef 2012-04-09 16:55:30