ios - 当我滚动 tableview 时,UITableViewCell 会更改数据和设计

标签 ios objective-c uitableview

我已阅读相关答案,但没有任何效果。当第一次加载表格时,一切都很好,但是当我滚动表格后,一切都改变了。如果有人可以帮忙吗?这是我的代码。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [[arrReportList objectAtIndex:section]count];
}

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

  SummaryReportTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"summaryCell"];
if (cell == nil) {
    cell = [[[NSBundle mainBundle] loadNibNamed:@"SummaryReportTableViewCell" owner:self options:nil] objectAtIndex:0];
}    
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell setCellLayoutWithArray:[[arrReportList objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]];
...
...
}

SummaryReportTableViewCell.m

-(void)setCellLayoutWithArray :(NSMutableArray *)array {

[btnViewPieChart setImage:[CommonMethod createIconWithIconName:@"fa-pie-chart" ForegroundColor:kColorEnableIcon BGColor:[UIColor clearColor] size:CGSizeMake(25, 25)] forState:UIControlStateNormal];

btnDomain.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
btnDomain.titleLabel.textAlignment = NSTextAlignmentLeft;
btnDomain.contentEdgeInsets = UIEdgeInsetsMake(0, 8, 0, 0);
[btnDomain setTitle:[array valueForKey:@"DomainName"] forState:UIControlStateNormal];

[btnGoal setTitle:[array valueForKey:@"Goals"] forState:UIControlStateNormal];
[self UIButton:btnGoal withValue:btnGoal.titleLabel.text];
}

-(void)UIButton :(UIButton *)btn withValue:(NSString *)value {
if ([btn.titleLabel.text integerValue]  > 0) {
    [btn setBackgroundColor:kColorTabMenuBG];
    btn.titleLabel.font = kFontBold(16);
    btn.titleLabel.textColor = kColorTabDeActiveMenu;
    btn.userInteractionEnabled = true;
} else {
    [btn setBackgroundColor:[UIColor whiteColor]];
    btn.titleLabel.font = kFontNormal(16);
    btn.titleLabel.textColor = [UIColor blackColor];
    btn.userInteractionEnabled = false;
}
}

第一次上图

enter image description here

再次向下和向上滚动表格后的数据相同

enter image description here

最佳答案

我可以看到问题出在单元格中。

原因:每当您将一个单元格出队时,它都会进入 tableview 的出队池并请求先前出队的单元格, 因此,当您向上滚动前一个出队单元格的完整数据时,新单元格将出现,并且您为新单元格所做的任何更改(数据源)都会反射(reflect)出来,如果您不更改任何属性、字段或数据源,它们将与前一个单元格保持相同。

解决方案:您正在为新单元格重新设置数据,因此值正常。创建一个单元格模型或数据源,您还可以在其中添加单元格的背景颜色,以便它为每个单元格重置或根据您正在制作的任何业务逻辑显式更改颜色。

关于ios - 当我滚动 tableview 时,UITableViewCell 会更改数据和设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47747038/

相关文章:

ios - 添加新单元格时,UItableview 上一个单元格消失

ios - 在两个 UIViewController 之间使用 UIStoryBoardSegue 进行重复的 segue 处理

iphone - iOS 保存/保留数据的位置 didReceiveMemoryWarning(等效 Android Bundle onSaveInstanceState)

ios - 如何删除 socket 的所有引用

objective-c - stringByReplacingOccurrencesOfString 正则表达式

ios - 界面测试失败 : Multiple Matches Found Error

ios - xcode 中.strings 文件中的pragma 标记替代是什么?

ios - iPhone 4s 和 iPhone 5 中的位置更新无法在后台模式 iOS 7.0 中运行,而手机处于理想状态

iphone - 从 Objective-C 中的另一个类调用方法

Xcode Imageview Aspect Fit 删除多余空间