IOS UiTableView Cells 我怎样才能让它的高度可调
IOS UiTableView Cells how can I make it's height adjustable
我正在使用 TableView 并有一个 TableViewCell,这是我第一次成功创建一个。我通过 Json 获取数据并填充 TableView;我的问题是所有 TableView 单元格都具有相同的高度,并且由于我返回的数据大小不同,因此导致一些看起来很糟糕的表格单元格。例如,看看下面的图片,如果像下面第一个单元格那样有更多数据,TableView 单元格会扩展,如果像下面第二个单元格那样数据较少,TableView 单元格就会收缩,这样就没有那么多白色 space显示。
StoryBoard 的外观如下所示,有时包含大量数据的元素是 Post 按钮标签,如下所示。我实际上是在尝试做两件事:如果 Post-Data 很长,则扩展 TableViewCell;如果 Post-data 很小,则使 TableViewCell 变小。
而且我可以访问所有元素
class HomePageTVC: UITableViewCell {
@IBOutlet weak var profile_id: UILabel!
@IBOutlet weak var comment: UIButton!
@IBOutlet weak var vote: UIButton!
@IBOutlet weak var time: UILabel!
@IBOutlet weak var post: UIButton!
@IBOutlet weak var fullname: UIButton!
@IBOutlet weak var location: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
profile_id.isHidden = true
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// post.isUserInteractionEnabled = true
// Configure the view for the selected state
}
}
第一步:
如图所示应用约束,
下图中显示的约束的文本表示是:)
垂直约束:
V:|-[8]-[全名]-[8]-[时间]-[8]-[TextView]-[8]-|
任何组件都没有高度限制。
在应用这些约束时,xCode 将建议您将 内容压缩阻力优先级 修改为 749 执行!
第 2 步:取消选中可滚动的 textView 属性
第3步:设置行数为0
第 4 步:在您的 tableView 控制器的 ViewDidLoad() 中写入
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 100
步骤 5:不要写 heightForRowAtIndexPath 委托 :)
就这些:)
希望对您有所帮助
除了 Sandeep 的回答之外,您应该在显示之前计算单元格高度,以防止出现一些滞后的滚动体验。
@interface SomeTableViewController ()
@property (strong, nonatomic) NSMutableDictionary *cellHeightsDictionary;
@end
@implementation SomeTableViewController
- (NSMutableDictionary *)cellHeightsDictionary { //setter
if (!_cellHeightsDictionary) {
_cellHeightsDictionary = [[NSMutableDictionary alloc]init];
}
return _cellHeightsDictionary;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
NSIndexPath *key = indexPath;
NSNumber *height = @(cell.frame.size.height);
//store the pre-calculated cell height and index path to the dictionary
[self.cellHeightsDictionary setObject:height forKey:key];
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSNumber *height = [self.cellHeightsDictionary objectForKey:indexPath];
if (height) { //load the height from dictionary for this index path
return height.floatValue;
}
return UITableViewAutomaticDimension;
}
@end
我正在使用 TableView 并有一个 TableViewCell,这是我第一次成功创建一个。我通过 Json 获取数据并填充 TableView;我的问题是所有 TableView 单元格都具有相同的高度,并且由于我返回的数据大小不同,因此导致一些看起来很糟糕的表格单元格。例如,看看下面的图片,如果像下面第一个单元格那样有更多数据,TableView 单元格会扩展,如果像下面第二个单元格那样数据较少,TableView 单元格就会收缩,这样就没有那么多白色 space显示。
StoryBoard 的外观如下所示,有时包含大量数据的元素是 Post 按钮标签,如下所示。我实际上是在尝试做两件事:如果 Post-Data 很长,则扩展 TableViewCell;如果 Post-data 很小,则使 TableViewCell 变小。
而且我可以访问所有元素
class HomePageTVC: UITableViewCell {
@IBOutlet weak var profile_id: UILabel!
@IBOutlet weak var comment: UIButton!
@IBOutlet weak var vote: UIButton!
@IBOutlet weak var time: UILabel!
@IBOutlet weak var post: UIButton!
@IBOutlet weak var fullname: UIButton!
@IBOutlet weak var location: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
profile_id.isHidden = true
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// post.isUserInteractionEnabled = true
// Configure the view for the selected state
}
}
第一步: 如图所示应用约束,
下图中显示的约束的文本表示是:)
垂直约束: V:|-[8]-[全名]-[8]-[时间]-[8]-[TextView]-[8]-|
任何组件都没有高度限制。 在应用这些约束时,xCode 将建议您将 内容压缩阻力优先级 修改为 749 执行!
第 2 步:取消选中可滚动的 textView 属性
第3步:设置行数为0
第 4 步:在您的 tableView 控制器的 ViewDidLoad() 中写入
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 100
步骤 5:不要写 heightForRowAtIndexPath 委托 :)
就这些:)
希望对您有所帮助
除了 Sandeep 的回答之外,您应该在显示之前计算单元格高度,以防止出现一些滞后的滚动体验。
@interface SomeTableViewController ()
@property (strong, nonatomic) NSMutableDictionary *cellHeightsDictionary;
@end
@implementation SomeTableViewController
- (NSMutableDictionary *)cellHeightsDictionary { //setter
if (!_cellHeightsDictionary) {
_cellHeightsDictionary = [[NSMutableDictionary alloc]init];
}
return _cellHeightsDictionary;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
NSIndexPath *key = indexPath;
NSNumber *height = @(cell.frame.size.height);
//store the pre-calculated cell height and index path to the dictionary
[self.cellHeightsDictionary setObject:height forKey:key];
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSNumber *height = [self.cellHeightsDictionary objectForKey:indexPath];
if (height) { //load the height from dictionary for this index path
return height.floatValue;
}
return UITableViewAutomaticDimension;
}
@end