Ios 如何排列不同的UITableViewCell图像和标签?

Ios 如何排列不同的UITableViewCell图像和标签?,ios,swift,uitableview,autolayout,Ios,Swift,Uitableview,Autolayout,我在我的单元格for rowat中所做的就是设置UITableViewCell的(无子类)imageView?.image和textLabel?.text值(以及字体和颜色)。根据我的模型通过SF符号和文本设置图像。更详细地说,只有三种可能的单元格类型:工作区、“添加工作区”按钮和存档 在下面的屏幕截图中,我用绿线演示了这些视图如何不以逻辑方式排列。文本在所有三种类型的单元格之间都不对齐,而且(令人烦恼的是)带有“添加”(+)指示器的方形图标的SF符号图像比标准图像稍宽 有谁能帮我简单地解决我刚

我在我的
单元格for rowat
中所做的就是设置UITableViewCell的(无子类)imageView?.image和textLabel?.text值(以及字体和颜色)。根据我的模型通过SF符号和文本设置图像。更详细地说,只有三种可能的单元格类型:工作区、“添加工作区”按钮和存档

在下面的屏幕截图中,我用绿线演示了这些视图如何不以逻辑方式排列。文本在所有三种类型的单元格之间都不对齐,而且(令人烦恼的是)带有“添加”(+)指示器的方形图标的SF符号图像比标准图像稍宽

有谁能帮我简单地解决我刚刚错过的问题吗?我已经尝试过使用约束将ImageView的纵横比设置为1:1。那没有影响任何事情

override func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
let单元格:UITableViewCell
如果indexath.section==0{
cell=tableView.dequeueReusableCell(带有标识符:“workspace”,for:indexath)
如果indexath.row==addWorkspaceRow{
cell.imageView?.image=.addWorkspace
cell.imageView?.tintColor=cell.tintColor
cell.textlab?.text=“添加工作区”
cell.textlab?.textColor=cell.tintColor
cell.accessoryType=.none
}否则{
让workspace=model.workspaces[indexPath.row]
cell.imageView?.image=.workspace
cell.imageView?.tintColor=.label
cell.textlab?.text=workspace.displayName
单元格.textLabel?.textColor=.label
cell.accessoryType=.disclosureIndicator
}
}否则{
cell=tableView.dequeueReusableCell(标识符为“archive”,for:indexath)
cell.imageView?.image=.archive
cell.imageView?.tintColor=.archive
cell.textlab?.text=“存档”
单元格.textLabel?.textColor=.label
cell.accessoryType=.disclosureIndicator
}
cell.textlab?.font=.preferredFont(forTextStyle:.body)
返回单元
}

更改
UIImageView
contentMode
属性将实现以下功能:

cell.imageView?.contentMode = .scaleAspectFit

发生这种情况是因为图像本身的尺寸不正确。

更改
UIImageView
contentMode
属性将实现以下目的:

cell.imageView?.contentMode = .scaleAspectFit

这是因为图像本身的尺寸不正确。

您在tableviewCell中的图像是错误的contentMode。这是由于您的标签布局错误造成的

您可以选择修复该问题:

选项1

打开文件XIB tableviewCell并更改UIImage的内容模式

选项2

您可以通过代码进行设置:
yourUImage.contentMode=.scaleAspectFit

您在tableviewCell中的图像是错误的contentMode。这是由于您的标签布局错误造成的

您可以选择修复该问题:

选项1

打开文件XIB tableviewCell并更改UIImage的内容模式

选项2

您可以通过代码进行设置:
yourUImage.contentMode=.scaleAspectFit

我花了很长时间寻找您描述的问题的解决方案,但遗憾的是,我没有找到任何解决方案。问题似乎是
UITableViewCell
中的本机
UIImageView
。基本上有两种解决方法:

A)将用作表格视图单元格图标的所有图像调整为特定宽度(例如30和44)。绘制此空间并将图标居中(同时保持原始大小)

B)习惯苹果的“你可以使用我们的预构建解决方案,但如果你想改变它,哪怕是一点点,你也必须从下到上重新构建一切”——理念,在应用程序中所做的一切都只需使用自定义单元即可。使用自定义单元格,您可以简单地将imageView宽度设置为某个值


我想知道他们是如何在iOS邮件应用程序中解决这个问题的。

我花了很长时间寻找您描述的问题的解决方案,但遗憾的是,我没有找到任何解决方案。问题似乎是
UITableViewCell
中的本机
UIImageView
。基本上有两种解决方法:

A)将用作表格视图单元格图标的所有图像调整为特定宽度(例如30和44)。绘制此空间并将图标居中(同时保持原始大小)

B)习惯苹果的“你可以使用我们的预构建解决方案,但如果你想改变它,哪怕是一点点,你也必须从下到上重新构建一切”——理念,在应用程序中所做的一切都只需使用自定义单元即可。使用自定义单元格,您可以简单地将imageView宽度设置为某个值


我想知道他们是如何在iOS邮件应用程序中解决这个问题的。

我遇到了同样的问题,希望不用自定义单元格就能解决它。但如果不将约束子类化,则很难配置约束

最后,我转向使用自定义单元格,它使用起来很简单,如果您可能需要在tableView单元格中添加更多内容,自定义单元格是更好的选择

我的代码,是的,我现在正在制作音乐播放器;)
我遇到了同样的问题,希望不使用自定义单元格来解决它。但如果不将约束子类化,则很难配置约束

最后,我转向使用自定义单元格,它使用起来很简单,如果您可能需要在tableView单元格中添加更多内容,自定义单元格是更好的选择

我的代码,是的,我现在正在制作音乐播放器;)
伙计,你只需要玩弄约束就行了。将“添加”图像的约束设置为与默认(黑色)图像相同。伙计,你只需使用约束即可。将“添加”图像的约束设置为与默认(黑色)图像相同
class LibraryViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    var tableView = UITableView()
    let cellTitles = ["Playlists", "Artists", "Albums", "Songs", "Genres"]
    let imageNames = ["music.note.list", "music.mic", "square.stack", "music.note", "guitars"]

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .white
        title = "Library"
        
        navigationController?.navigationBar.prefersLargeTitles = true
        
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(LibraryTableCell.self, forCellReuseIdentifier: "libraryTableCell")
        tableView.translatesAutoresizingMaskIntoConstraints = false
        tableView.rowHeight = 48
        view.addSubview(tableView)
        
        
        let g = view.safeAreaLayoutGuide
        NSLayoutConstraint.activate([
            tableView.leadingAnchor.constraint(equalTo: g.leadingAnchor),
            tableView.trailingAnchor.constraint(equalTo: g.trailingAnchor),
            tableView.topAnchor.constraint(equalTo: g.topAnchor, constant: 20),
            tableView.heightAnchor.constraint(equalTo: g.heightAnchor, multiplier: 0.6)
        ])
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return cellTitles.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "libraryTableCell", for: indexPath) as? LibraryTableCell else {
            fatalError("Unable to dequeue libraryTableCell")
        }
        
        cell.accessoryType = .disclosureIndicator
        
        let imageName = imageNames[indexPath.row]
        cell.cellImageView.image = UIImage(systemName: imageName)
        
        let title = cellTitles[indexPath.row]
        cell.cellLabel.text = title
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        cell.separatorInset = UIEdgeInsets(top: 8, left: 20, bottom: 8, right: 8)
    }
}