单元格隐藏属性[tableView]

时间:2013-09-10 15:58:49

标签: ios objective-c

我有部分:1有11行。我设置这样的高度

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) return 350;
    else if (indexPath.section == 1) {
        return 150;
    }
    else return 0;
}

如果我设置cell.hidden = YES;我如何删除单元格,因为如果cell.hidden = YES它将显示空间但我不想要我想要的空间如果隐藏单元格直接从secrion删除:1

或者如果我可以为0.0

设置每个隐藏单元格的高度

由于

修改

没有用于tableView的NSArray,它是像这样自定义的

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {


    if (section == 0) { return 1; }
    else if (section == 1) {

        int retype = [type intValue];

        if (retype == 1 || retype == 5 || retype == 10) {
            return 0;
        }

        else if (retype == 11) { return 9; }
        else if (retype == 14) { return 4; }

        else {

            return 11;
        }

    }
    else return 0;

}

并且像这样配置单元格

static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            cell.selectionStyle = UITableViewCellSelectionStyleGray;
        }

int truef = [*nsstring* intValue];
if (indexPath.section == 1) {
if (indexPath.row == 0) {
if (truef == 0) {
cell.hidden = YES;
}
else {
cell.titleLabel.text = @"True";
}
}
}

编辑2

使用deleteRowsAtIndexPaths后的错误

reason: 'Invalid update: invalid number of rows in section 1.  The number of rows contained in an existing section after the update (11) must be equal to the number of rows contained in that section before the update (11), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out)

2 个答案:

答案 0 :(得分:1)

您不需要隐藏单元格。这里适当的方法是从dataSource中删除此单元格,并通过

将其从表格视图中删除
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];

修改

嗯......你会同意我的说法,你使用的“神奇数字”并不是好方法。 我在这里看到以下解决方案:您将所有返回的号码放在

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

到实例变量中,比如

@property(assign,nonatomic)numberOfRowsInSection0;

比你可以减少这个变量并删除最后一行。在您的情况下,您将只能从表格视图中删除最后一行。

结论:上面的解决方案只是一个hack,它允许您只删除表中的最后一行。您必须再次检查代码并更改逻辑以使用阵列或任何其他存储。表视图假定您有一些数据源,但是您没有,这是您问题的核心。

答案 1 :(得分:0)

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
if(cell.hidden) { return 0.0f; }