Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom UITableViewCells disappearing when scrolling
    primarykey
    data
    text
    <p>I am making an app in which I have MKMapView with some MKAnnotations on it. I also have a list (a UITableView) with more info about places represented by those annotations, in which my problem occurs. I use my own UITableViewCell subclass, which has 3 UILabels: the default textLabel for name of the place, addressLabel and phoneNumberLabel. I have to consider situation where texts in those labels are too big to be displayed in one line and resize the UILabels accordingly. I also have to consider situation, where there is no phone number or address and according UILabels should "disappear" (frame size is 0).</p> <p>Problem is, when I scroll down the table, cells disappear from the upper part of the screen (the further I scroll, the more of them are invisible, and for most of the UITableView there are no cells displayed at all, just section headers, even though the size of the whole table doesn't change). </p> <p>(screenshot was supposed to go here, but stackoverflow didn't allow me to post it :( ) ![enter image description here][1]</p> <p>Here's my custom UITableViewCell subclass:</p> <pre><code>@implementation POICell - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Initialization code self.addressLabel = [[UILabel alloc] init]; self.phoneNumberLabel = [[UILabel alloc] init]; [self addSubview:self.addressLabel]; [self addSubview:self.phoneNumberLabel]; self.addressLabel.font = [UIFont fontWithName:@"Helvetica" size:14]; self.phoneNumberLabel.font = [UIFont fontWithName:@"Helvetica" size:14]; self.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20]; self.addressLabel.lineBreakMode = UILineBreakModeWordWrap; self.phoneNumberLabel.lineBreakMode = UILineBreakModeWordWrap; self.textLabel.lineBreakMode = UILineBreakModeWordWrap; } return self; } - (void)layoutSubviews { [super layoutSubviews]; CGRect titleFrame = self.frame; titleFrame.origin.x += 30; titleFrame.size.width -= 60; titleFrame.size.height = [self.textLabel.text sizeWithFont:self.textLabel.font forWidth:self.textLabel.frame.size.width lineBreakMode:NSLineBreakByWordWrapping].height; self.textLabel.frame = titleFrame; if (self.addressLabel.text != nil) { self.addressLabel.frame = CGRectMake(titleFrame.origin.x, [DDUtility yRelativeTo:self.textLabel withMargin:0], titleFrame.size.width, [self.addressLabel.text sizeWithFont:self.addressLabel.font forWidth:self.addressLabel.frame.size.width lineBreakMode:NSLineBreakByWordWrapping].height); } else { self.addressLabel.frame = CGRectMake(titleFrame.origin.x, [DDUtility yRelativeTo:self.textLabel withMargin:0], 0, 0); } NSString *labelText = self.addressLabel.text; if (self.phoneNumberLabel.text != nil) { self.phoneNumberLabel.frame = CGRectMake(titleFrame.origin.x, [DDUtility yRelativeTo:self.addressLabel withMargin:0], titleFrame.size.width, [self.phoneNumberLabel.text sizeWithFont:self.phoneNumberLabel.font forWidth:self.phoneNumberLabel.frame.size.width lineBreakMode:NSLineBreakByWordWrapping].height); } else { self.phoneNumberLabel.frame = CGRectMake(titleFrame.origin.x, [DDUtility yRelativeTo:self.addressLabel withMargin:0], 0, 0); } labelText = self.phoneNumberLabel.text; } </code></pre> <p>And here part of my UIViewController, which is both a delegate and datasource for 2 of my tables - categoryTable works just fine, it's poiTable I have problems with.</p> <p>poiArraysDictionary is a dictionary in which keys are names of the Point of Interest categories and values are arrays of points of this category. Points themselves are dictionaries I get as JSONs from server, with keys like name, address (can be null) and phone (can be null).</p> <pre><code>- (void) viewDidLoad { (...) UITableView *poiTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 120, listView.frame.size.width, listView.frame.size.height - 120) style:UITableViewStylePlain]; poiTable.separatorStyle = UITableViewCellSeparatorStyleNone; //poiTable.rowHeight = 80; poiTable.dataSource = self; poiTable.delegate = self; poiTable.backgroundColor = [UIColor clearColor]; self.poiTable = poiTable; [poiTable reloadData]; [self.listView addSubview:poiTable]; (...) } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { if (tableView == self.categoryTable) { return 1; } else { return [self.categoryArray count]; } } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (tableView == self.categoryTable) { return [self.categoryArray count]; } else { return [[self.poiArraysDictionary objectForKey:[self.categoryArray objectAtIndex:section]] count]; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (tableView == self.categoryTable) { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"categoryCell"]; if (cell == nil) { cell = [[[CategoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"categoryCell"] autorelease]; } cell.textLabel.text = [self.categoryArray objectAtIndex:indexPath.row]; cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.backgroundColor = [UIColor clearColor]; return cell; } else { POICell *cell = [tableView dequeueReusableCellWithIdentifier:@"poiCell"]; if (cell == nil) { cell = [[[POICell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"poiCell"] autorelease]; } /*int maxIndex = [self.pointsOfInterestArray count]; int index; int indexInSection = 0; for (index = 0; index &lt; maxIndex; ++index) { if ([((NSString*)([((NSDictionary*)([self.pointsOfInterestArray objectAtIndex:index])) objectForKey:@"category"])) isEqualToString:[self.categoryArray objectAtIndex:indexPath.section]]) { if (indexInSection != indexPath.row) { ++indexInSection; } else { break; } } } cell.textLabel.text = [[self.pointsOfInterestArray objectAtIndex:index] objectForKey:@"name"]; if ([[[self.pointsOfInterestArray objectAtIndex:index] objectForKey:@"address"] class] != [NSNull class]) { cell.addressLabel.text = [[self.pointsOfInterestArray objectAtIndex:index] objectForKey:@"address"]; } if ([[[self.pointsOfInterestArray objectAtIndex:index] objectForKey:@"phone"] class] != [NSNull class]) { cell.phoneNumberLabel.text = [[self.pointsOfInterestArray objectAtIndex:index] objectForKey:@"phone"]; }*/ if ([[[[self.poiArraysDictionary objectForKey:[self.categoryArray objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row] objectForKey:@"name"] class] == [NSNull class]) { return nil; } cell.textLabel.text = [[[self.poiArraysDictionary objectForKey:[self.categoryArray objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row] objectForKey:@"name"]; if ([[[[self.poiArraysDictionary objectForKey:[self.categoryArray objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row] objectForKey:@"address"] class] == [NSNull class]) { cell.addressLabel.text = nil; } else { cell.addressLabel.text = [[[self.poiArraysDictionary objectForKey:[self.categoryArray objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row] objectForKey:@"address"]; } if ([[[[self.poiArraysDictionary objectForKey:[self.categoryArray objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row] objectForKey:@"phone"] class] == [NSNull class]) { cell.phoneNumberLabel.text = nil; } else { cell.phoneNumberLabel.text = [@"telefon: " stringByAppendingString:[[[self.poiArraysDictionary objectForKey:[self.categoryArray objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row] objectForKey:@"phone"]]; } cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.backgroundColor = [UIColor clearColor]; CGRect cellFrame = cell.frame; return cell; } } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { if (tableView == self.categoryTable) { return 0; } else { return 40; } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (tableView == self.categoryTable) { return 50; } else { NSDictionary *point = [[self.poiArraysDictionary objectForKey:[self.categoryArray objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row]; CGFloat rowHeight = 0; if ([[point objectForKey:@"name"] class] != [NSNull class]) { rowHeight += [[point objectForKey:@"name"] sizeWithFont:[UIFont fontWithName:@"Helvetica-Bold" size:20] forWidth:self.poiTable.frame.size.width lineBreakMode:NSLineBreakByWordWrapping].height; } if ([[point objectForKey:@"address"] class] != [NSNull class]) { rowHeight += [[point objectForKey:@"address"] sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14] forWidth:self.poiTable.frame.size.width lineBreakMode:NSLineBreakByWordWrapping].height; } if ([[point objectForKey:@"phone"] class] != [NSNull class]) { rowHeight += [[point objectForKey:@"phone"] sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14] forWidth:self.poiTable.frame.size.width lineBreakMode:NSLineBreakByWordWrapping].height; } return rowHeight; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload