我试图调整我的表格视图中的一个单元格的高度。我正在调整单元格的“大小检查器”内的“行高”设置的大小。当我在我的iPhone上运行应用程序时,单元格的默认大小设置自表格视图中的“行大小”。
如果我改变了表格视图的“行大小”,那么所有单元格的大小都会改变。我不想这样做,因为我只想为一个单元格自定义大小。我已经看到了很多关于这个问题的程序化解决方案的帖子,但如果可能的话,我更喜欢通过故事板来实现。
我试图调整我的表格视图中的一个单元格的高度。我正在调整单元格的“大小检查器”内的“行高”设置的大小。当我在我的iPhone上运行应用程序时,单元格的默认大小设置自表格视图中的“行大小”。
如果我改变了表格视图的“行大小”,那么所有单元格的大小都会改变。我不想这样做,因为我只想为一个单元格自定义大小。我已经看到了很多关于这个问题的程序化解决方案的帖子,但如果可能的话,我更喜欢通过故事板来实现。
当前回答
鉴于我没有通过Interface Builder找到这个问题的任何解决方案,我决定在Swift中使用两个动态单元发布一个编程解决方案,即使最初的问题要求通过Interface Builder解决问题。不管怎样,我认为这对Stack Overflow社区是有帮助的:
import UIKit
enum SignInUpMenuTableViewControllerCellIdentifier: String {
case BigButtonCell = "BigButtonCell"
case LabelCell = "LabelCell"
}
class SignInUpMenuTableViewController: UITableViewController {
let heightCache = [SignInUpMenuTableViewControllerCellIdentifier.BigButtonCell : CGFloat(50),
SignInUpMenuTableViewControllerCellIdentifier.LabelCell : CGFloat(115)]
private func cellIdentifierForIndexPath(indexPath: NSIndexPath) -> SignInUpMenuTableViewControllerCellIdentifier {
if indexPath.row == 2 {
return SignInUpMenuTableViewControllerCellIdentifier.LabelCell
} else {
return SignInUpMenuTableViewControllerCellIdentifier.BigButtonCell
}
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return self.heightCache[self.cellIdentifierForIndexPath(indexPath)]!
}
...
}
其他回答
对于动态单元格,UITableView上设置的rowHeight总是覆盖单个单元格的rowHeight。如果内容在行内,只需计算动态高度。
我认为这是一个bug。
尝试调整高度不是通过实用工具检查器,而是直接通过鼠标拖动故事板。
我用这个方法解决了这个问题。
我最近一直在纠结这个问题。我的问题是上面发布的解决方案使用highightforrowatindexpath:方法可以在模拟器中的iOS 7.1中工作,但如果简单地切换到iOS 8.1,就会完全搞砸结果。
我开始阅读更多关于自调整单元格的内容(在iOS 8中引入,阅读这里)。很明显,在iOS 8中使用UITableViewAutomaticDimension会有所帮助。我尝试使用这种技术并删除了highightforrowatindexpath:瞧,它现在在iOS 8中完美地工作了。但是iOS 7并没有。我该怎么办呢?我需要heightForRowAtIndexPath: iOS 7,而不是iOS 8。
以下是我的解决方案(为简洁起见进行了修改),借鉴了@JosephH上面发布的答案:
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.estimatedRowHeight = 50.;
self.tableView.rowHeight = UITableViewAutomaticDimension;
// ...
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
return UITableViewAutomaticDimension;
} else {
NSString *cellIdentifier = [self reuseIdentifierForCellAtIndexPath:indexPath];
static NSMutableDictionary *heightCache;
if (!heightCache)
heightCache = [[NSMutableDictionary alloc] init];
NSNumber *cachedHeight = heightCache[cellIdentifier];
if (cachedHeight)
return cachedHeight.floatValue;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
CGFloat height = cell.bounds.size.height;
heightCache[cellIdentifier] = @(height);
return height;
}
}
- (NSString *)reuseIdentifierForCellAtIndexPath:(NSIndexPath *)indexPath {
NSString * reuseIdentifier;
switch (indexPath.row) {
case 0:
reuseIdentifier = EventTitleCellIdentifier;
break;
case 2:
reuseIdentifier = EventDateTimeCellIdentifier;
break;
case 4:
reuseIdentifier = EventContactsCellIdentifier;
break;
case 6:
reuseIdentifier = EventLocationCellIdentifier;
break;
case 8:
reuseIdentifier = NotesCellIdentifier;
break;
default:
reuseIdentifier = SeparatorCellIdentifier;
break;
}
return reuseIdentifier;
}
SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@“8.0”)实际上来自我正在使用的一组宏定义,这是我在某处找到的(非常有用)。它们被定义为:
#define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
如果你用的是swift,就像这样用。不要使用故事板来选择行高。像这样通过编程设置表行高,
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.row == 0 || indexPath.row == 1{
let cell = self.tableView.dequeueReusableCellWithIdentifier("section1", forIndexPath: indexPath) as! Section1TableViewCell
self.tableView.rowHeight = 150
cell.label1.text = "hiiiiii"
cell.label2.text = "Huiiilllllll"
return cell
} else {
let cell = self.tableView.dequeueReusableCellWithIdentifier("section2", forIndexPath: indexPath) as! Section2TableViewCell
self.tableView.rowHeight = 60
cell.label3.text = "llll"
return cell
}
}
在动态单元格上,UITableView上设置的rowHeight总是覆盖单个单元格的rowHeight。
但是在静态单元格上,在单个单元格上设置rowHeight可以覆盖UITableView的。
不确定这是否是一个漏洞,苹果可能是故意这么做的?