TableView Row Height Wrong on 64-bit Only

解决问题还是要google啊,今天着实感觉到了与百度的区别。

下面是转自pete(User Pete - Stack Overflow)的文章,解释了app切换到64位运行时,tableview的显示问题。原因是tableview相关设置高度等方法,返回值类型错误。

CGFloat 原先定义的是float的typedef,64位后定义为double。

下面是原文,有兴趣的可以研究。

附地址:iOS – TableView Row Height Wrong on 64-bit Only

iOS – TableView Row Height Wrong on 64-bit Only

I had a fun little problem pop up at my client site today. We had a tableview that was displaying data that looked perfect when running on everything except for an iPhone 5s. After a little bit of DuckDuckGo-ing, we came across these Stack Overflow questions here and here that suggested that the heightForRowAtIndexPath method might be the culprit.

When I went and dug in, I found this warning (I did do some cut and paste to get the warning just over near the code):

TableView Row Height Wrong on 64-bit Only_第1张图片

If you can’t read it, that warning says, “Conflicting return type in implementation of ‘tableView:heightForRowAtIndexPath:': ‘CGFloat’ (aka ‘double’) vs ‘float'”

The code was the following:

view plaincopy to clipboardprint?

- (float) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

if(indexPath.row % 2 == 0) {

return247.0;

}

else{

return315.0;

}

}

The signature for that method means that the code should have looked like this:

view plaincopy to clipboardprint?

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

if(indexPath.row % 2 == 0) {

return247.0;

}

else{

return315.0;

}

}

So what is the explanation? Basically CGFloat was typedef’ed as float. However, Apple wanted you to use CGFloat in case they ever changed what a CGFloat was going to stand for. Apparently, with the move to 64-bit, Apple has done just that. In 32 bit versions, the signatures were identical. However, now in 64-bit land, because the method was returning the wrong type, iOS considered the return value to be 0, breaking our layout. By only changing the return type in the signature to the proper value, the app functioned again properly.

Keep this in mind the next time you might think about being smarter than the language designers and consider not using their typedefs.

你可能感兴趣的:(TableView Row Height Wrong on 64-bit Only)