在viewWillAppear中更新UItableView?

时间:2013-05-16 10:51:56

标签: ios ipad uitableview viewwillappear

我的UItableview存在问题,当我每次查看表时使用viewWillAppear检索更新的数据时,这会让我感到异常。

任何人都可以帮我了解如何刷新viewWillAppear中的tableview,以便在调用CellforrowAtindexPath之前更新它吗?

以下是viewWillAppear的代码:

-(void)viewWillAppear:(BOOL)animated
{

     [super viewWillAppear:YES];
     [self.tableView reloadData];//table was filled in viewDidLoad.

}

这是CellforrowAtindexPath` :(例外是在mainArray上,这意味着通过viewWillAppear和调用单元更新表时存在冲突):

//Methods to retrive all selected words , thier sound and picture for the mother
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifer = @"Cell";
    UITableViewCell *cell= [ tableView dequeueReusableCellWithIdentifier:CellIdentifer];

    cell.textLabel.font = [UIFont systemFontOfSize:17.0];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:CellIdentifer];
    }
    cell.textLabel.font = [UIFont systemFontOfSize:17.0];
    UILabel *label ;

    label=(UILabel *)[cell viewWithTag:1];

    NSString *value = [[self.mainArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    label.text = value;
    label.textAlignment=NSTextAlignmentRight;
    label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;

    //self.tableView.separatorColor = [UIColor blackColor];TRY TO SEPARETE BETEWEEN LINES OF TABLE
    UIButton *butt =(UIButton *)[cell viewWithTag:2];
    NSString *value2 = [[self.mainArray2 objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    butt.restorationIdentifier= value2;
    [butt addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];

    NSString *value3 = [[self.mainArray3 objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    cell.imageView.image = [UIImage imageNamed:value3];
    label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;

    return cell;
}

1 个答案:

答案 0 :(得分:0)

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; {

return self.mainArray.count; }

如果count为0,表视图将不会调用其他函数。