当我尝试创建一个新单元时,UITableview上的所有单元格都会发生变化

时间:2013-03-17 02:18:58

标签: iphone ios objective-c uitableview

我正在尝试创建一个iOS应用程序,其中我必须有一个UITableView,每当我按下一个新的输入按钮时,会出现一个newcell,按下该按钮的时间。我的问题是,每当我按下按钮时,不仅创建的单元格显示当前时间,而且其上方的单元格显示不同的时间,重新加载并显示当前时间。为了更好地解释它,如果我在8:05,9:01和9:10按下按钮,我想要显示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

3 个答案:

答案 0 :(得分:0)

你的问题就在这一行:

 cell.textLabel.text = [_entryArray lastObject];

您需要使用:

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

或者,

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

答案 1 :(得分:0)

cell.textLabel.text = [_entryArray lastObject]将只返回数组中的最后一个对象,这就是为什么你看到重复的同一时间。将其更改为:

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

这应该可以解决潜在的问题。

答案 2 :(得分:0)

[_ entryArray lastObject]总是给出最后一个返回的对象。

使用

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