user3146380
user3146380

Reputation: 61

Array values repeated whenever tableview scrolls

I am doing one application.In that i am showing the 10 sessions.Each session contain one value.I am getting these values from array.Initially it shows the 4 values per screen.Whenever i scroll the tableview i want to show the remaining values for remaining sessions.But it showing the first values again.So please tell me how to show each value for each session.

Upvotes: 0

Views: 491

Answers (3)

gruhm
gruhm

Reputation: 63

Owen is correct. The referenced article is misleading in that the examples only populate the cell data when a new cell is created and not upon dequeueing and reusing a cell. Try making your code look more like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{   
    UITableViewCell* cell;
    float cellFontSize = 24;
    float cellFontOffset = 1;

    cell = [tableView dequeueReusableCellWithIdentifier:@"wordList"];

    if(nil == cell)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"wordList"];

    cell.textLabel.font = [UIFont fontWithName:@"Saddlebag" size:cellFontSize];
    cell.textLabel.textColor = WW_DARKBLUE;
    cell.textLabel.text = (NSString *)[_gameManager.wordList objectAtIndex:indexPath.row];
    cell.backgroundColor = [SKColor clearColor];
    cell.textLabel.shadowColor = WW_YELLOW;
    cell.textLabel.shadowOffset = CGSizeMake(cellFontOffset, cellFontOffset);
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
}

The difference is that you are not only setting the cell properties when it is new, you also set the properties of the dequeued reusable cells. Notice that the values are being set in both cases of the conditional and not only within a larger else block. Hope that make sense.

Upvotes: 1

Owen Hartnett
Owen Hartnett

Reputation: 5935

The key is in your cellForRowAtIndexPath method. Whenever it is called, you must refresh the data for that row (based on the IndexPath) for the cell you are going to return. What's happening is that the cell is scrolled off the top, your cellForRowAtIndexPath method is providing a new cell by reusing the cell that just got scrolled off, and it's still got the old data in that cell. The solution is to refresh the data in the cell every time cellForRowAtIndexPath is called, especially when you're reusing the cell. Avoid the accepted answer in the referenced stack overflow article, as it does not properly re-use the cells in the approved manner (which is why it has a minus rating).

Upvotes: 2

aslana
aslana

Reputation: 20

If you write code snippets it can be more clear. In my understanding you want to remove used sessions from tableview. If so, you can remove that sessions from array or store them to new array called usedArray then reload tableView.

Upvotes: 0

Related Questions