2013-05-16 27 views
0

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

请问任何人可以帮助我如何刷新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; 
} 
+2

将代码放在'[super viewWillAppear:YES]'之上;'是一种不好的做法。顺便说一下,'cellforrowAtindexPath'总是在'reloaddata'后面调用。你得到什么异常?在调用'[self.tableView reloadData]'之前填充'self.mainArray'是否为 –

+0

? –

+0

@VaibhavSaran是的我知道这是坏的,但我读过一次,它会在CellforrowAtindexPath之前调用?我得到的例外是,该阵列超出索引 – user2323070

回答

0
  • (NSInteger的)的tableView:(UITableView的*)的tableView numberOfRowsInSection:(NSInteger的)部分; {

return self.mainArray.count; }

如果count为0,table view将不会调用其他函数。

+0

它不是零仍然 – user2323070