gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:38:03 25 4
gpt4 key购买 nike

我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com