ios - 选择单元格时 TableViewCell 内容重复

标签 ios objective-c uitableview xcode6

我有一个 TableView 正在调用单独的UITableViewCell。我有代码在 UITableViewCell 中布局单元格,并且它在我的表格 View 中正确显示。但出于某种原因,如果我选择单元格,单元格中的内容将在单元格顶部重新绘制。

有人知道为什么会这样吗?我已附上用于在下面绘制单元格的代码。

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

if (tableView == productInformationTable){

    if (indexPath.row == 0) {
        static NSString *CellIdentifier = @"Cell";
        ProductImagesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[ProductImagesTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }
        else{
            cell = [[ProductImagesTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }
        return cell;
    }

    else if (indexPath.row == 1) {
        static NSString *CellIdentifier = @"Cell";
        ProductQuantityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        if (cell == nil) {
            cell = [[ProductQuantityTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }
        else{
            cell = [[ProductQuantityTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }
        return cell;
    }

    else if (indexPath.row == 2) {
        static NSString *CellIdentifier = @"Cell";
        ProductButtonsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[ProductButtonsTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }
        else{
            cell = [[ProductButtonsTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }
        return cell;
    }



    else {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }
        else{
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }
        return cell;
    }
}

else {
    return nil;
}

我手机的代码如下 -

- (void)awakeFromNib {

    }




- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    }

- (void)layoutSubviews
{
    [super layoutSubviews];

    self.contentView.backgroundColor = [UIColor redColor];
    customButton = [[SAExpandableButton alloc]initWithFrame:CGRectMake(self.frame.size.width - 50, self.frame.size.height/2 - 15, 30, 30)];
    customButton.layer.borderColor = [Styles priceRed].CGColor;
    customButton.layer.borderWidth = 1.5;
    customButton.layer.cornerRadius = 15;
    customButton.expandDirection = SAExpandDirectionLeft;
    customButton.numberOfButtons = 8;
    customButton.selectedIndex = 0;

    quantityLabel = [UILabel new];
    quantityLabel.frame = CGRectMake(10, self.frame.size.height/2 - 10, 100, 20);
    quantityLabel.text = @"Quantity";
    [self addSubview:quantityLabel];


    customButton.buttonTitles = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8", nil];

    [self addSubview:customButton];



}



- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
if(cell.selectionStyle == UITableViewCellSelectionStyleNone){
    return nil;
}
return indexPath;
NSLog(@"willSelectRow called");

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@"Table Pressed");

}

最佳答案

您不能在 layoutSubviews 中创建单元格的 subview ,因为它可以被调用任意次数。使用 initWithStyle:reuseIdentifier: 进行初始化,并将它们布局在 layoutSubviews(因此得名)中。

此外,您必须为所有不同的细胞类型使用不同的 CellIdentifier!

编辑:

要防止选择单元格,您应该使用 tableView:willSelectRowAtIndexPath: 并返回 nil 以防止选择。

关于ios - 选择单元格时 TableViewCell 内容重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26134517/

相关文章:

ios - Swift 在 UITableView 中创建分页

ios - 在主线程上重新加载 TableView 导致崩溃 - iOS

swift - prepareForSegue 中的函数不会执行

android - 包含对 AndroidX 和旧支持库的引用

ios - productsRequest 不适用于应用内购买

ios - IOS 9远程通知中的滑动按钮 - 当应用程序在后台而不是在应用程序内存不足时工作

ios - 过滤服务器响应 RestKit + CoreData

objective-c - UIKit 中是否有内置事件处理的基/实用程序类?

ios - 更改 Safari View Controller 完成按钮色调?

ios - 解决中心崩溃报告中的执行顺序错误