UITableView reuseIdentifier: repeating elements at top and bottom

I'm having trouble with my UITableView top and bottom cells alternating whenever they scroll off and back onto screen. I believe this is an issue with reuseIdentifier but I'm not sure how to get around it. PlaceTableViewCell is a subclass of ABTableViewCell from Loren Britcher for fast scrolling.

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

    static NSString *uniqueIdentifier = @"Cell";

    PlaceTableViewCell *cell = (PlaceTableViewCell *)[tableView dequeueReusableCellWithIdentifier:uniqueIdentifier];


    if(cell == nil){
       cell = [[PlaceTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:uniqueIdentifier];
    }

    Places *place = [self.placesArray objectAtIndex:[indexPath row]];    
    cell.place_title = place.title;
    cell.place_street = place.street;
    cell.place_thumb = place.thumb_path;


    return cell;

}

Edit:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.placesArray count];
}

From my subclass of ABTableViewCell:

- (void)drawContentView:(CGRect)r
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    UIColor *backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
    UIColor *textColor = [UIColor blackColor];

    if(self.selected)
    {
        backgroundColor = [UIColor clearColor];
        textColor = [UIColor whiteColor];
    }

    [backgroundColor set];
    CGContextFillRect(context, r);

    CGPoint p;
    p.x = 42;
    p.y = 2;

        NSLog(@"place_thumb: %@", place_thumb);

        NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:place_thumb]];
        UIImage *image = [UIImage imageWithData:imageData];
        [image drawAtPoint: p];


        p.x = p.x + 50;

        [textColor set];
        [place_title drawAtPoint:p withFont:place_titleFont];

        p.y = 25;

        textColor = [UIColor lightGrayColor];
        [textColor set];
    [place_street drawAtPoint:p withFont:place_streetFont];

}

Answers


Your cellForRowAtIndexPath looks to be correct. You need to post all your data source methods for the UITableView. I have a hunch that the issue might be happening in your numberOfRows method.

Are you setting the number of rows based on the array size? Also, are you sure your array is remaining unchanged?


Need Your Help

iOS: best way to encode/decode memory intensive objects

objective-c ios cocoa-touch memory-management data-persistence

I'm fairly new to iOS programming and am struggling to decide on what is the best way to encode memory intensive objects using the NSCoding protocol.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.