0

I have read the related answers but nothing is being worked. When table load for the first time, everything is ok but after I scroll the table everything's changed. If anyone can help please? here is my code.

- (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;
}
}

Image for the first time

enter image description here

Same data after I scroll the table down and up again

enter image description here

Krutika Sonawala
  • 1,065
  • 1
  • 12
  • 30
  • Just reset your button ui changes in setCellLayoutWithArray. Eg. if you have change font to bold on click, reset to regular. – Jay Gajjar Dec 11 '17 at 05:46
  • @Krutika it's an issue of Reusability. You've to load, unload & changes design based on your requirement whenever `cellForRowAtIndexPath` gets called. – Kiran Jasvanee Dec 11 '17 at 05:47
  • @KiranJasvanee you mean the UI chnages I have written in cell view controller, needs to be there in main view controller in cellForRow method? – Krutika Sonawala Dec 11 '17 at 06:18
  • @KrutikaSonawala Not necessary. But you have to keep track of design changes too somewhere in your array or model inside array. Suppose check your first time image, in skipped skills -> first 2 cells have 1 & 2 highlighted, 3rd cell is not. So you whenever this cells going to appear in the screen of iPhone/iPad they recall `cellForRowAtIndexPath` for design changes and data. So you have to keep track and show highlighted when it's required, but also have to keep code of unhighlight, when you don't need to highlight it. – Kiran Jasvanee Dec 11 '17 at 06:26
  • @KrutikaSonawala the code in your question is ok. You should provide more code in `SummaryReportTableViewCell.m` and `cellForRowAtIndexPath` method. – trungduc Dec 11 '17 at 06:42
  • @KrutikaSonawala try to debug your btnWithValues Method and you will find your answer there. just check what value you are passing and inside method what value you are receiving. or if you are using pagination in this tableview so also check will display method implementation – Ravi Panchal Dec 11 '17 at 06:59

1 Answers1

1

I can see the issue is of color in cells.

Reason: Whenever you dequeue a cell it goes to tableview's dequeue pool and ask for the previous dequeued cell, Hence when you scroll up the full data for previous dequeued cell comes for new cell and what ever changes(datasource) you make for new cell reflects and if you don't change any property, field or datasource that will remain same as previous cell's.

Solution: Data you are setting again for new cell hence values are coming fine. Make a cellmodel or a data source where you add cell's background color also so that it resets for every cell or explicitly change color based on whatever business logic you are making.

Mridul Gupta
  • 599
  • 5
  • 18