【问题标题】:Update UItableView in viewWillAppear?在 viewWillAppear 中更新 UItableView?
【发布时间】:2013-05-16 10:51:56
【问题描述】:

我的UItableview 有问题,当我使用viewWillAppear 检索每次查看表时更新的数据时,这给了我例外。

请任何人帮助我如何刷新viewWillAppear 中的表格视图,以便在调用CellforrowAtindexPath 之前更新它?

这是我的viewWillAppear 代码:

-(void)viewWillAppear:(BOOL)animated
{

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

}

这里是 CellforrowAtindexPath` :(mainArray 上的异常,这意味着通过 viewWillAppear 更新表格和调用 cell 存在冲突):

//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;
}

【问题讨论】:

  • 将代码放在[super viewWillAppear:YES]; 之上是一种不好的做法。顺便说一句,CellforrowAtindexPath 总是在reloaddata 之后调用。你得到了什么例外?
  • 在调用 [self.tableView reloadData] 之前填充了 self.mainArray 吗?
  • @VaibhavSaran 是的,我知道这很糟糕,但我读过它会在 CellforrowAtindexPath 之前被调用?我得到了数组超出索引的异常
  • @AhmedZ. 是的。但是我有一个例外是超出索引的数组
  • 您的self.mainArray 一定缺少某些东西。检查它是否超出索引。 cellForRowAtIndexPath 正在调用,则无需调用reloaddata !!

标签: ios ipad uitableview viewwillappear


【解决方案1】:
  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;{

返回 self.mainArray.count; }

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-16
    • 2014-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多