0

新しいエントリボタンを押すたびに、そのボタンを押した時間とともに newcell が表示される UITableView が必要な iOS アプリを作成しようとしています。私の問題は、ボタンを押すたびに、作成されたセルに現在の時刻が表示されるだけでなく、別の時刻を表示していたその上のセルがリロードされ、現在の時刻も表示されることです。わかりやすく説明すると、8:05、9:01、および 9:10 にボタンを押すと、UITableView に次のように表示されます。

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

代わりに、次のように表示されます。

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

私は何をしますか??ありがとう

これが私のコードです( newEntry はボタンで、brain は現在の時刻を取得する方法があるオブジェクトです)

@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
4

3 に答える 3

0

あなたの問題はこの行にあります:

 cell.textLabel.text = [_entryArray lastObject];

使用する必要があります:

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

または、

cell.textLabel.text = _entryArray[indexPath.row];
于 2013-03-17T02:21:48.830 に答える
0

cell.textLabel.text = [_entryArray lastObject]は配列内の最後のオブジェクトのみを返すため、同じ時間が繰り返されます。これを次のように変更します。

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

これにより、根本的な問題が修正されるはずです。

于 2013-03-17T02:40:17.253 に答える
0

[_entryArray lastObject] は、常に最後に返されたオブジェクトを提供します。

使用する

cell.textLabel.text = [_entryArray objectAtIndex: indexPath.row];
于 2013-03-17T02:42:08.780 に答える