自上浆(动态高度)在iOS 8的细胞 - 可能没有自定义的UITableViewCell? [英] Self-Sizing (Dynamic Height) Cells in iOS 8 - Possible without Custom UITableViewCell?

查看:179
本文介绍了自上浆(动态高度)在iOS 8的细胞 - 可能没有自定义的UITableViewCell?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能自我大小的UITableViewCell iOS中8,而无需创建自定义的UITableViewCell?

我认为标准的UITableViewCell类型(UITableViewCellStyleDefault,UITableViewCellStyleSubtitle,UITableViewCellStyleValue1,UITableViewCellStyleValue2)已经建立汽车布局约束。这是由以下事实非定制单元的限制不能在故事板被改变确认。

但是当我使用类型UITableViewCellStyleValue1的非自定义单元格,在故事板将其设置为为textLabel和detailTextLabel设置numberOfRows为0,并设置viewDidLoad中code如下,只有细胞为textLabel是占在电池的高度的自动调整大小。如果detailTextLabel结束上显示比为textLabel更多行,为detailTextLabel文本在电池的顶部和底部边缘溢出的。此外,细胞做为正常为textLabel调整,但似乎忽略了detailTextLabel在调整的过程。

主要的事情,我需要知道的是 - 我需要甚至如果我想正确地支持动态文本和自我调整大小,可以使用标准电池的行创建一个自定义单元格

   - (无效)viewDidLoad中{    [超级viewDidLoad中];    [self.tableView setEstimatedRowHeight:DEFAULT_ROW_HEIGHT];
    [self.tableView setRowHeight:UITableViewAutomaticDimension];
}


解决方案

我刚刚在iOS中尝试这个10 / X code 8与不同的细胞类型(iOS中9 / X code 7相同的结果)它看起来像它可能只为为textLabel而不是为detailTextLabel。

(基本上重复了OP中提到的问题)

的ViewController code交替设置了一些文字上detailTextLabel和为textLabel。

 类的ViewController:UIViewController的,的UITableViewDelegate,UITableViewDataSource {
    @IBOutlet弱VAR的tableView:UITableView的!    覆盖FUNC viewDidLoad中(){
        super.viewDidLoad()
        tableView.estimatedRowHeight = 44
        tableView.rowHeight = UITableViewAutomaticDimension
    }    FUNC的tableView(_的tableView:UITableView的,cellForRowAt indexPath:IndexPath) - GT; {的UITableViewCell
        让细胞= tableView.dequeueReusableCell(withIdentifier:细胞,为:indexPath)
        如果indexPath.row%2 == 0 {
            cell.textLabel?的.text =Lorem存有悲坐阿梅德,consectetur adipiscing ELIT,sed的做eiusmod tempor incididunt UT labore等dolore麦格纳aliqua。
            ?cell.detailTextLabel的.text =< - 为textLabel
        }其他{
            ?cell.textLabel的.text =detailTextLabel - >中
            cell.detailTextLabel?的.text =Lorem存有悲坐阿梅德,consectetur adipiscing ELIT,sed的做eiusmod tempor incididunt UT labore等dolore麦格纳aliqua。
        }
        回报细胞
    }    FUNC的tableView(_的tableView:UITableView的,numberOfRowsInSection段中:int) - GT; INT {
        返回10
    }}

请确保你为textLabel和textDetailLabel的路线属性设置为0,这里的结果。

基本单元

右键详细细胞

左详细细胞

字幕细胞

我会作为一个bug报告这一情况。

Is it possible to self-size a UITableViewCell in iOS 8 without creating a Custom UITableViewCell?

I thought that the standard UITableViewCell types (UITableViewCellStyleDefault, UITableViewCellStyleSubtitle, UITableViewCellStyleValue1, UITableViewCellStyleValue2) had built in auto layout constraints. This is confirmed by the fact that the constraints for non-custom cells cannot be changed in Storyboard.

But when I use a non-custom cell of type UITableViewCellStyleValue1, set it up in Storyboard, set numberOfRows for textLabel and detailTextLabel to 0, and set the viewDidLoad code as below, only the textLabel of the cell is accounted for in the autosizing of the height of the cell. If the detailTextLabel ends up displaying on more lines than the textLabel, the text for detailTextLabel spills out over the top and bottom edges of the cell. Again, the cell does resize properly for the textLabel, but seems to ignore the detailTextLabel in its resizing process.

The main thing I need to know is - do I need to create a custom cell even for the rows that can use a standard cell if I want to properly support Dynamic Text and Self-Sizing?

- (void)viewDidLoad {

    [super viewDidLoad];

    [self.tableView setEstimatedRowHeight:DEFAULT_ROW_HEIGHT];
    [self.tableView setRowHeight:UITableViewAutomaticDimension];
}

解决方案

I just tried this in iOS 10/XCode 8 (same results in iOS 9/XCode 7) with the different cell types and it looks like it's possible ONLY for the textLabel and not for the detailTextLabel.

(basically repeating the issue that the OP mentioned)

ViewController code that sets some text alternately on detailTextLabel and textLabel.

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.estimatedRowHeight = 44
        tableView.rowHeight = UITableViewAutomaticDimension
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        if indexPath.row % 2 == 0 {
            cell.textLabel?.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
            cell.detailTextLabel?.text = "<- textLabel"
        } else {
            cell.textLabel?.text = "detailTextLabel ->"
            cell.detailTextLabel?.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
        }
        return cell
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

}

Make sure you set the textLabel and textDetailLabel's line property to 0 and here are the results.

Basic Cell

Right Detail Cell

Left Detail Cell

Subtitle Cell

I'll report this as a bug.

这篇关于自上浆(动态高度)在iOS 8的细胞 - 可能没有自定义的UITableViewCell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆