Quote:
Originally Posted by TimC
I have a UItableview with cells. Some cells have uilabels and some have uibuttons. However, the uibuttons repeat when i scroll down (appearing over the uilabel).. and then multiply over the uilabels when I scroll up. Any clues why?
|
Sounds like you are having cell reuse problems.
When you call the dequeue method, the system hands you a cell that your program used before, but scrolled off-screen. That used cell will be set up however you set it up before.
If you have different kinds of cells that have different field layouts, you should make each different type use a different reuse identifier (which is passed in initWithStyle:reuseIdentifier: and in dequeueReusableCellWithIdentifier)
In your example, have a type
Code:
#define K_CELL_WITH_BUTTONS @"CellWithButtons"
#define K_CELL_WITH_LABELS @"CellWithLabels"
When you create a cell with buttons, use code like this:
Code:
MyButtonCell* theCell;
theCell = [tableView dequeueReusableCellWithIdentifier: K_CELL_WITH_BUTTONS];
if (theCell == nil)
theCell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault
reuseIdentifier: K_CELL_WITH_BUTTONS] autorelease];
That way the system will only give you a "recycled" cell that is set up with buttons. It wouldn't give you a cell that you set up with labels but no buttons, because that type of cell would have a different reuse identifier.
When you use table views, you need to make sure you understand cell reuse. You want the code that CREATES a cell separate from the code that configures cells. The code that creates a cell sets up the fields inside the cell.
The code that configures a cell assumes that it starts from a cell with the correct structure, but explicitly sets the state of all the fields, including clearing out values that aren't used. It can't assume that the contents of the cell are in their default state, since the cell may be a recycled cell that has left-over contents from the last time the cell was used (it's like re-using a paper form. You have to erase all the old fields on the form before you can start filling it out. If you don't have any kids, but the last person who filled out the form had kids, you have to erase the info about kids from the form before you can fill out your info)