自定义UItableView无法在ios8上正确显示

时间:2014-06-08 20:09:25

标签: ios objective-c uitableview

我制作了一个自定义UITableViewCell,当我显示它时,我有这个结果(我在iPhone 5上运行xcode 6和iOS 8 beta 1.)

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

当我旋转设备,然后旋转回肖像,一切都变得正确。

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

请注意,当我使用xcode 5编译我的应用程序时,我没有遇到任何问题。

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;

谢谢!

5 个答案:

答案 0 :(得分:10)

您需要从CGFloat返回tableView:heightForRowAtIndexPath:。如果从同一方法返回浮点数,则会出现您看到的错误:

//causes the bug
-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    //...
}

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

答案 1 :(得分:2)

这个问题有一个公认的答案,但我遇到了同样的问题,这个问题并没有被接受的答案所解决,该答案告诉我们要将float更改为CGFloat

我发现拥有基于表格rowHeight的代码的人也可能遇到此问题。在iOS8中,重要的是要estimatedRowHeight而不仅仅是rowHeight

当我在编程生成的UITableView中使用这些变量时,修复这些变量可以解决问题。

答案 2 :(得分:1)

我可能有点迟到了,但我今天遇到了与iOS 8 beta 2和xCode 6 beta 2完全相同的问题(2014年6月26日),我发现他们仍然没有出现过这样的问题修复了这个错误。

还有xCode 6 beta的另一个错误,那就是我的应用无法正确请求GPS位置 - 它根本不会要求获得许可。这也可以通过切换回xCode 5来解决。我认为目前使用xCode 5构建和测试应用程序更加可行。

答案 3 :(得分:0)

尝试重复使用单元格,例如,更改:

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

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;

答案 4 :(得分:0)

我有一个类似的问题并通过使用修复它     cell.clipsToBounds = YES