reloadData只刷新一行

时间:2013-02-01 20:41:42

标签: iphone ios objective-c xcode ipad

我有一个4行的分组tableView。我对两个视图之间的操作进行了相当大的改革,现在reloadData表现得很奇怪。它只会刷新一行数据。 (最后一个)而不是其他人。

我在viewDidAppear方法(我调用reloadData)中检查了我的值,并更新了所有值。

代码..

 - (void)viewDidAppear:(BOOL)animated
{
   [super viewWillAppear:animated];
   // reload the table data
   [self.tableView reloadData];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{     
   // Set up the cell...
  static NSString *CellWithIdentifier = @"Cell";
  UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];
if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellWithIdentifier];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = [_tableGroup.options objectAtIndex:rowcount];
    cell.tag = rowcount;
    rowcount++;

    //label for currently selected/saved setting
    _currentSetting = [[BaseLabel alloc] initWithFrame:CGRectMake(160, 8, 115, 25)];
    [_currentSetting setFont:[UIFont systemFontOfSize:14]];
    _currentSetting.backgroundColor = [UIColor clearColor];
    _currentSetting.textColor = [UIColor blueColor];
    _currentSetting.textAlignment = NSTextAlignmentRight;
}

if (cell.tag == 0) {
    _currentSetting.text = [NSString stringWithFormat:@"%@ mi",[settings.val1 stringValue]];

}
else if(cell.tag == 1)
{
    _currentSetting.text = [NSString stringWithFormat:@"%@ items",[settings.val2 stringValue]];

}
else if(cell.tag == 2) 
{
    _currentSetting.text = [NSString stringWithFormat:@"%@ items",[settings.val3 stringValue]];

}

[cell.contentView addSubview:_currentSetting];

return cell;
}

我已经完成了NSLog,并且所有内容都被调用,因为它应该在reloadData上调用,但是单元格没有更改它们的标签。为什么呢?

3 个答案:

答案 0 :(得分:3)

我在这里可以看到的主要问题是:

cell.tag = rowcount;
rowcount++;
if(cell == nil)中,

cell.tag不能出现,你应该把它取出来。相反,你不应该检查cell.tag来设置currentSettingText,你应该使用indexPath.row

同样在每个viewDidAppear中,当你重新加载Table时,会调用cellForRowAtIndexPath,每次都会递增rowCount,从而递增cell.tag,你在哪里重置它?

答案 1 :(得分:3)

问题1: cellForRowAtIndexPath需要通过获取现有单元格,或者通过创建单元格并添加子视图(仅在创建单元格时)来返回单元格。当我们有一个现有的单元格时,我们可以假设它已经添加了子视图,所以我们只是去寻找它。所以它会像这样工作......

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {     

    static NSString *CellWithIdentifier = @"Cell";
    BaseLabel *_currentSetting;

    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellWithIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        // new cell, so add a label
        _currentSetting = [[BaseLabel alloc] initWithFrame:CGRectMake(160, 8, 115, 25)];
        [_currentSetting setFont:[UIFont systemFontOfSize:14]];
        _currentSetting.backgroundColor = [UIColor clearColor];
        _currentSetting.textColor = [UIColor blueColor];
        _currentSetting.textAlignment = NSTextAlignmentRight;
        _currentSetting.tag = 128;
        [cell.contentView addSubview:_currentSetting];
    } else {
        // existing cell so find the label
        _currentSetting = (BaseLabel *)[cell viewWithTag:128];
    }

问题2:现在我们准备好了一个单元格和标签子视图的句柄,它应该如何配置?唯一合理的方法是根据indexPath查看我们的模型。

在没有理解模型的情况下,我无能为力,但通用的想法是:

    // say my model is an array of N arrays, one for each section, I would do this
    NSArray *sectionModel = [self.mainModel objectAtIndex:indexPath.section];
    id *modelElement = [sectionModel objectAtIndex:indexPath.row];

    // now configure the cell based on modelElement
    _currentSetting.text = [modelElement someStringAttributeOfMyModel];
    return cell;
}

答案 2 :(得分:1)

我可以忽略viewWillAppear中的viewDidAppear来电,这很奇怪,但有趣的是你根本不使用indexPath参数。 由于此参数是如何检查生成的单元格的唯一方法,因此几乎可以获得任何结果,并且可以随机排序单元格。 reloadData只会再次洗牌。

由于rowcount仅增加,很快您的cell.tag ==比较都不会评估为真。

顺便说一句,代码是将子视图添加到已经存在的单元格中 - 这意味着在多次重新加载或滚动之后,您的单元格将有许多标签。为什么将添加到单元格中的最后一个标签保存到实例变量中,更改文本然后将其移动到另一个单元格...这是个谜。

您的代码没有任何意义!

相关问题