2013-03-17 46 views
0

我正嘗試創建一個iOS應用程序,其中必須有一個UITableView,每次按下新的輸入按鈕時,按下該按鈕。我的問題是,每次按下按鈕時,不僅所創建的單元格顯示當前時間,而且顯示不同時間的單元格將重新加載並顯示當前時間。爲了嘗試更好地解釋它,如果我按下按鈕在8:05,9:01和9:10,我想的UITableView顯示:當我嘗試創建一個新的單元格時,UITableview上的所有單元格都會發生更改

-8:05 
-9:01 
-9:10 

相反,它顯示:

-9:10 
-9:10 
-9:10. 

我該怎麼做?由於

這裏是我的代碼(newEntry是按鈕和大腦是一個對象,我必須得到當前時間的方法)

@implementation MarcaPontoViewController{ 

    NSMutableArray *_entryArray; 
@synthesize brain=_brain; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    _brain = [[Brain alloc] init]; 
    _entryArray = [[NSMutableArray alloc] init]; 

    //[self updateTime]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 
    return 1; 
    } 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return [_entryArray count]; 
} 

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *CellIdentifier= @"myCell"; 

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

    cell.textLabel.text = [_entryArray lastObject]; 
      } 

    return cell; 
} 


- (IBAction)newEntry:(id)sender { 


    [_entryArray addObject:[self.brain currentTime]]; 


    [_timeTable reloadData]; 

} 

@end 

回答

0

你的問題是在這裏在這一行:

cell.textLabel.text = [_entryArray lastObject]; 

您需要使用:

cell.textLabel.text = [_entryArray objectAtIndex:indexPath.row]; 

或者,

cell.textLabel.text = _entryArray[indexPath.row]; 
+0

謝謝主席先生,你的幫助是非常apreciated。它現在工作:) – dietbacon 2013-03-17 02:42:49

0

cell.textLabel.text = [_entryArray lastObject]將永遠只返回數組中的最後一個對象,這就是爲什麼你看到重複的時間相同的原因。將其更改爲:

// in cellForRowAtIndexPath: 
cell.textLabel.text = [_entryArray objectAtIndex:indexPath.row]; 

這應該解決潛在的問題。

0

[_entryArray lastObject]總是給出最後返回的對象。

使用

cell.textLabel.text = [_entryArray objectAtIndex: indexPath.row]; 
相關問題