简体   繁体   中英

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];

}

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM