查看更改UITableView更新

时间:2014-02-25 13:17:40

标签: ios uitableview uicollectionview uipickerview

我在UITableView中有一个相当复杂的表格。此表单在表格视图单元格中有一些UICollectionView,还有一些选择器显示与日历应用程序相同的方式:

Image

现在,当我在我的应用上执行此操作时,我UICollectionView中的一个比它开始时更高 - 并且它继续前进(我添加了一个红色边框以确保它是UICollectionView正在调整大小):

Image

我尝试调试UICollectionView视图属性,但它永远不会更改(并且它的getter / setter调用次数不超出我的预期) - 即使我在cellForRowAtIndexPath打印它时它确实显示了调整大小。有什么方法可以更好地调试这个,或者有人处于同样的情况吗?

谢谢!

代码:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//self.campos is a dictionary with sections and rows stored
    NSMutableArray *infoCampos = [[self.campos objectAtIndex:indexPath.section] objectForKey:@"campos"];
NSDictionary *infoCampo = [infoCampos objectAtIndex:indexPath.row];

if ([[infoCampo objectForKey:@"nome"] isEqualToString:@"dosePorComprimido"]) {
    //Remove picker cell if finds it
    for (infoCampo in infoCampos) {
        if ([infoCampo objectForKey:@"view"] == self.dosagemMedicamento.view) {
            [infoCampos removeObject:infoCampo];
            [tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
            return;
        }
    }

    //Insert new cell
    infoCampo = [infoCampos objectAtIndex:indexPath.row];
    [infoCampos addObject:@{@"tipo":@5, @"view":self.dosagemMedicamento.view, @"nome":[infoCampo objectForKey:@"nome"]}];
    [tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.dosagemMedicamento.view.superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[pickerDosagem]|" options:0 metrics:nil views:@{@"pickerDosagem":self.dosagemMedicamento.view}]];
}
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *infoCampo = [[[self.campos objectAtIndex:indexPath.section] objectForKey:@"campos"] objectAtIndex:indexPath.row];
    UIView *view;

    if ([infoCampo objectForKey:@"view"]) {
        view = (UIView *) [infoCampo objectForKey:@"view"];
        return view.frame.size.height;
    }

    return 44.0f;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:

(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    NSMutableDictionary *infoCampo = [[[self.campos objectAtIndex:indexPath.section] objectForKey:@"campos"] objectAtIndex:indexPath.row];
    UIView *view;

cell = [tableView dequeueReusableCellWithIdentifier:[[infoCampo objectForKey:@"nome"] stringByAppendingString:@"ViewLinhaCellView"] forIndexPath:indexPath];

        view = [cell viewWithTag:1];

        [view addSubview:[infoCampo objectForKey:@"view"]];

return cell;
}

编辑:忘了提到我没有使用reloadData更新表视图,而只使用reloadRowsAtIndexPaths和reloadSections来更新所需的部分/行。所以这使得它更加怪异,因为我没有重新加载那个特定的部分。

编辑2:添加了数据源e委托代码

1 个答案:

答案 0 :(得分:2)

我认为你的问题出现在tableView:heightForRowAtIndexPath:

的实施中
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *infoCampo = [[[self.campos objectAtIndex:indexPath.section] objectForKey:@"campos"] objectAtIndex:indexPath.row];
    UIView *view;

    if ([infoCampo objectForKey:@"view"]) {
        view = (UIView *) [infoCampo objectForKey:@"view"];
        return view.frame.size.height;
    }

    return 44.0f;
}

视图可能会通过自动布局或其自动调整遮罩调整大小。这里没有足够的信息来确切说明原因,但是表视图和自动布局可能会有一些有趣的行为。我建议只返回你设置的高度,而不是从视图中获取高度:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *infoCampo = [[[self.campos objectAtIndex:indexPath.section] objectForKey:@"campos"] objectAtIndex:indexPath.row];
    UIView *view;

    if ([infoCampo objectForKey:@"view"]) {
        if ([infoCampo objectForKey:@"viewHeight"]) {
            return [[infoCampo objectForKey:@"viewHeight"] floatValue];
        } else {
            return 216.0f;
        }
    }

    return 44.0f;
}

viewHeight键允许您为每个单元格的视图指定自定义高度,但此实现还返回默认高度(216),该高度恰好是选择器视图的标准高度。