2013-03-17 60 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]; 
相关问题