Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-17-2010, 07:43 AM   #1 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 41
TimC is on a distinguished road
Default UiTableView cells repeating problem

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?

Last edited by TimC; 06-17-2010 at 09:14 AM.
TimC is offline   Reply With Quote
Old 06-17-2010, 10:54 AM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

in your cellForRowAtIndexpath:

you should only be adding buttons in the
if( cell == nil )
{
// add button
}
smithdale87 is offline   Reply With Quote
Old 06-17-2010, 12:14 PM   #3 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by TimC View Post
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)
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 07-16-2011, 07:02 AM   #4 (permalink)
Registered Member
 
Join Date: Apr 2011
Posts: 40
chings228 is on a distinguished road
Default

can i replace

Code:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
to

Code:
UITableViewCell *cell = [[[UITableViewCell alloc] init] autorelease];
it seems work but don't know any bad impact ?
chings228 is offline   Reply With Quote
Reply

Bookmarks

Tags
uitableview, uitableviewcell

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 326
8 members and 318 guests
Absentia, Domele, fiftysixty, givensur, heshiming, michaelhansen, PixelInteractive, Sloshmonster
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,892
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:16 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0