0

我在表格视图中有 3 个部分,并且仅使用中间部分第 2 部分来显示各种单元格。第 1 节和第 3 节只显示一个单元格,我将它们设为不可点击,因为我想在它们上面显示按钮和文本。我制作了它们,它工作正常,直到我制作了第 1 和第 3 节 userInteractionEnabled=NO。

代码:我知道我可以使这个面向对象,并且确实如此,但是一旦出现这个问题,我就尝试以不同的方式使它变得不同,但它仍然是一样的。

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

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:CGRectZero];
        selectedBackgroundView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.1];
        cell.selectedBackgroundView = selectedBackgroundView;
        if(cell==nil) { NSLog(@"Cell is nil");}
    }

    if(indexPath.section == 0)
    {
        cell.textLabel.text = nil;
        cell.accessoryView = nil;
        cell.detailTextLabel.text = nil;
        dosageButton                            = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        amountButton                            = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [dosageButton addTarget:self action:@selector(showDosages:) forControlEvents:UIControlEventTouchUpInside];
        [amountButton addTarget:self action:@selector(showAmount) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:dosageButton];
        [self.view addSubview:amountButton];
        cell.userInteractionEnabled = NO;
        return cell;
    }

    else if (indexPath.section == 1)
    {
        if (self.nameMutable.count != 0 )
        {
            cell.textLabel.text = [self.nameMutable objectAtIndex:indexPath.row];
            cell.detailTextLabel.text = [NSString stringWithFormat:@"$%@",[self.priceMutable objectAtIndex:indexPath.row]];
            cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"chevron"]];
            return cell;
        }
        else
        {
            //Empty for now. Waiting for data fetching to finish
            return cell;
        }
    }
    else if (indexPath.section == 2)
    {
        cell.userInteractionEnabled = NO;
        cell.accessoryView = nil;
        cell.detailTextLabel.text = nil;
        return cell;
    }
    else
    {
        NSLog(@"Something went wrong");
        return cell;
    }

}

出于某种原因,我在第 1 节中的表格视图单元格应该是可点击的,颜色变为深灰色并且不再可点击。它通常是单元格 3 和单元格 10。此外,当我向下滚动并且第 0 节不再可见,然后我向上滚动并且第 0 节可见时,一些单元格变得不可点击并且文本的颜色发生变化。

另外,如何使第 1 部分内的某个单元格具有更大的高度,因为文本太长而无法显示并且它开始显示“...”或覆盖了 detailTextLabel。提前致谢。

4

1 回答 1

0

您必须记住,这些单元格正在被重复使用或“回收”,因此如果您要设置userInteractionEnabled=NO语句if,则需要userInteractionEnabled=YESelse语句中设置它,或者YES在所有语句之前设置它。您还需要确保将某些索引路径特有的任何其他子视图(按钮等)添加到新创建的单元格,您可以将这段代码粘贴到if(cell==nil)语句中。像这样的东西:

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

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:CGRectZero];
        selectedBackgroundView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.1];
        cell.selectedBackgroundView = selectedBackgroundView;
        if(indexPath.section == 0) {
            dosageButton                            = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            amountButton                            = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            [dosageButton addTarget:self action:@selector(showDosages:) forControlEvents:UIControlEventTouchUpInside];
            [amountButton addTarget:self action:@selector(showAmount) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:dosageButton];
            [self.view addSubview:amountButton];
        }

        if(cell==nil) { NSLog(@"Cell is nil");}
    }

    cell.userInteractionEnabled = YES;
    cell.accessoryView = nil;
    cell.textLabel.text = nil;
    cell.detailTextLabel.text = nil;

    if(indexPath.section == 0)
    {
        cell.userInteractionEnabled = NO;
    }

    else if (indexPath.section == 1)
    {
        if (self.nameMutable.count != 0 )
        {
            cell.textLabel.text = [self.nameMutable objectAtIndex:indexPath.row];
            cell.detailTextLabel.text = [NSString stringWithFormat:@"$%@",[self.priceMutable objectAtIndex:indexPath.row]];
            cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"chevron"]];
        }
        else
        {
            //Empty for now. Waiting for data fetching to finish
        }
    }
    else if (indexPath.section == 2)
    {
        cell.userInteractionEnabled = NO;
    }
    else
    {
        NSLog(@"Something went wrong");
    }
    return cell;
}

如果你想改变某些索引路径的高度,(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath Docs委托方法。

于 2013-09-16T23:40:25.717 回答