custom UItableView not displaying correctly on ios8

匿名 (未验证) 提交于 2019-12-03 03:06:01

问题:

I made a custom UITableViewCell and when I display it, i have this result (I'm running xcode 6 and iOS 8 beta 1 on an iPhone 5.)

http://imgur.com/2MyT0mF,Nx1o4bl#0

And when I rotate my device, then rotate back to portrait, everything becomes correct.

http://imgur.com/2MyT0mF,Nx1o4bl#1

Note that when I was using xcode 5 to compile my app, I had no problems.

Code used in cellForRowAtIndexPath:

BGTCoursCell1 *cell = (BGTCoursCell1 *)[tableView dequeueReusableCellWithIdentifier:@"Cell"]; NSArray  *nib = [[NSBundle  mainBundle] loadNibNamed:@"BGTCoursCell1" owner:self options:nil]; cell = [nib objectAtIndex:0]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"HH:mm"]; NSLocale *locale = [NSLocale currentLocale]; [dateFormatter setLocale:locale];  if (indexPath.section == 0) {    BGTCours *cours = [[currentDay coursMatin] objectAtIndex:indexPath.row];     cell.matiereLabel.text = [cours matiere];     cell.salleLabel.text = [cours salle];     cell.heureLabel.text = [NSString stringWithFormat:@"De %@ à %@", [dateFormatter stringFromDate:[cours beginDate]], [dateFormatter stringFromDate:[cours endDate]]]; } else {    BGTCours *cours = [[currentDay coursAprem] objectAtIndex:indexPath.row];     cell.matiereLabel.text = [cours matiere];     cell.salleLabel.text = [cours salle];     cell.heureLabel.text = [NSString stringWithFormat:@"De %@ à %@", [dateFormatter stringFromDate:[cours beginDate]], [dateFormatter stringFromDate:[cours endDate]]]; } return cell; 

Thanks !

回答1:

You need to return a CGFloat from the tableView:heightForRowAtIndexPath: . The error you are seeing will appear if you return a float from the same method:

//causes the bug -(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {     //... }  //fixes the bug -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {     return tableView.rowHeight; // whatever } 


回答2:

This question has an accepted answer, but I have the same problem which was not fixed by the accepted answer which tells us to change float to CGFloat.

I found out that people who has code based on table's rowHeight may have this problem too. In iOS8 it is important to look at estimatedRowHeight not just rowHeight.

As I was using these variables in my programmatically generated UITableView, fixing those variables solved the problem.



回答3:

I might be a bit late to the party but I've encountered exactly same problem with iOS 8 beta 2 and xCode 6 beta 2 today(26-June-2014), I see they still haven't fixed the bug.

There's also another bug with xCode 6 beta, that is my app can no request the GPS location properly - it simply won't ask for the permission. This is also solved by switching back to xCode 5. I think at the moment is more viable to use xCode 5 to build and test your app.



回答4:

try reusing the cell, like, change:

BGTCoursCell1 *cell = (BGTCoursCell1 *)[tableView dequeueReusableCellWithIdentifier:@"Cell"]; NSArray  *nib = [[NSBundle  mainBundle] loadNibNamed:@"BGTCoursCell1" owner:self options:nil]; cell = [nib objectAtIndex:0]; 

to

static NSString *CellIdentifier = @"Cell"; static NSString *CellNib = @"BGTCoursCell1";  BGTCoursCell1 *cell = (BGTCoursCell1 *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if( cell == nil ) {     NSArray  *nib = [[NSBundle  mainBundle] loadNibNamed:CellNib owner:self options:nil];     cell = [nib objectAtIndex:0]; }  ... return cell; 


回答5:

i had a slightly similar problem and fixed it by using cell.clipsToBounds = YES



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!