UITableView 自定义单元格更改值 iOS 9
Posted
技术标签:
【中文标题】UITableView 自定义单元格更改值 iOS 9【英文标题】:UITableView Custom cell changing values iOS 9 【发布时间】:2016-01-10 08:34:21 【问题描述】:我在自定义单元格中遇到问题,同时上下滚动单元格中的值会发生变化。我在最后一节中添加了 2 个按钮。请检查以下代码。任何帮助,非常感谢。非常感谢。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *cellIdentifier = @"cell";
DrinkCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell)
cell = [[DrinkCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
if (indexPath.section == [tableView numberOfSections] - 1)
cell.tileImage.hidden = YES;
cell.pizzaTopins.hidden = YES;
// cell.accessoryType = UITableViewCellAccessoryNone;
if (indexPath.row == 0)
cell.separatorInset = UIEdgeInsetsMake(0.f, 0.f, 0.f, cell.bounds.size.width);
UILabel *calorieLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, cell.contentView.frame.size.height/2-30/2, self.tableView.frame.size.width, 30)];
calorieLabel.text = @"1000 Calories | 700 grams of fat";
cell.backgroundColor = backgroundColorApp;
[cell addSubview:calorieLabel];
else if (indexPath.row == 1)
cell.backgroundColor = backgroundColorApp;
NSLog(@"cell width : %f",cell.contentView.frame.size.width);
UIView *quantityView = [[UIView alloc]init];
quantityView.frame = CGRectMake(25 ,cell.contentView.frame.size.height/2-50/2, cell.contentView.frame.size.width-55, 50);
quantityView.backgroundColor = [UIColor whiteColor];
quantityView.clipsToBounds = YES;
quantityView.userInteractionEnabled = YES;
quantityView.layer.cornerRadius = 10.0f;
UIButton *decreaseButton = [[UIButton alloc]initWithFrame:CGRectMake(10, quantityView.frame.size.height/2-35/2, 35, 35)];
[decreaseButton setTitle:@"-" forState:UIControlStateNormal];
[quantityView addSubview:decreaseButton];
[decreaseButton addTarget:self action:@selector(decreaseQuantity:) forControlEvents:UIControlEventTouchUpInside];
[decreaseButton setTitleColor:quantityButtonColor forState:UIControlStateNormal];
[decreaseButton setTintColor:[UIColor greenColor]];
decreaseButton.clipsToBounds = YES;
decreaseButton.layer.cornerRadius = 17.5f;
decreaseButton.layer.borderColor = quantityButtonColor.CGColor;
decreaseButton.layer.borderWidth = 1.5f;
int i =0;
quantityLabel = [[UILabel alloc]initWithFrame:CGRectMake(quantityView.frame.size.width/2-30/2, quantityView.frame.size.height/2-30/2, 30, 30)];
quantityLabel.text = [NSString stringWithFormat:@"%d",i];
[quantityLabel setFont:[UIFont boldSystemFontOfSize:18]];
quantityLabel.textColor = quantityButtonColor;
[quantityView addSubview:quantityLabel];
UIButton *increaseButton = [[UIButton alloc]initWithFrame:CGRectMake(quantityView.frame.size.width-45, quantityView.frame.size.height/2-35/2, 35, 35)];
[increaseButton setTitle:@"+" forState:UIControlStateNormal];
[increaseButton addTarget:self action:@selector(increaseQuantity:) forControlEvents:UIControlEventTouchUpInside];
[quantityView addSubview:increaseButton];
increaseButton.clipsToBounds = YES;
[increaseButton setTitleColor:quantityButtonColor forState:UIControlStateNormal];
increaseButton.layer.cornerRadius = 17.5f;
increaseButton.layer.borderColor = quantityButtonColor.CGColor;
increaseButton.layer.borderWidth = 1.5f;
[cell addSubview:quantityView];
[quantityView addSubview:decreaseButton];
[quantityView addSubview:increaseButton];
else if (indexPath.row == 2)
addTocartButton = [[UIButton alloc]initWithFrame:CGRectMake(25, cell.contentView.frame.size.height/2 - 50/2, cell.contentView.frame.size.width-55, 50)];
[addTocartButton setTitle:@"Add to Cart" forState:UIControlStateNormal];
[addTocartButton addTarget:self action:@selector(addToCartOnCreatePizza:) forControlEvents:UIControlEventTouchUpInside];
addTocartButton.backgroundColor = [UIColor colorWithRed:190.0f/255.0f green:19.0f/255.0f blue:31.0f/255.0f alpha:1.0];
// [cell.contentView addSubview:addTocartButton];
[cell.contentView insertSubview:addTocartButton atIndex:0];
addTocartButton.clipsToBounds = YES;
addTocartButton.layer.cornerRadius =10.0f;
cell.pizzaTopins.textColor = [UIColor colorWithRed:190.0f/255.0f green:19.0f/255.0f blue:31.0f/255.0f alpha:1.0];
cell.pizzaTopins.text = [dataArray objectAtIndex:indexPath.row];
cell.tileImage.image = [UIImage imageNamed:@"profile44.png"];
// cell.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
return cell;
【问题讨论】:
您面临的具体问题是什么?没有得到你的问题。 滚动时,tableview 看起来很乱。按钮和标签已折叠。 如果我删除我在上一节中添加的按钮是没有问题的。但是如果我添加按钮,在向下和向上滚动时,按钮会出现在第一行。 可以在滚动之前/之后添加 2 个屏幕截图吗? 【参考方案1】:dequeueReusableCellWithIdentifier 将“重用”以前创建的单元格,因此您必须确保重新初始化所有手动添加/修改的属性,包括您以编程方式添加的任何按钮。
屏幕截图可以帮助我们正确诊断问题,但您的代码容易受到重复使用单元格的挥之不去的属性的影响,所以我建议从这个开始。
顺便说一句,如果您在 IB 中定义了多个单元原型并根据 indexPath 行改变单元标识符,那么您最终可能会得到更简单的代码。
【讨论】:
感谢您的回答。我已经通过在故事板中创建另一个客户单元来修复。以上是关于UITableView 自定义单元格更改值 iOS 9的主要内容,如果未能解决你的问题,请参考以下文章
如何使用文本字段 UITableView 从自定义单元格中获取值?
如何在自定义 UITableView 单元格上获取 UITextField 值
分组 UITableView 自定义绘制单元格,而不更改背景颜色?
更改单元格中的数据后,UITableView 中的自定义单元格将无法正确重新加载