在 xcode 6 中快速自定义表格视图单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25999759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:40:30  来源:igfitidea点击:

Custom table view cell with swift in xcode 6

iosxcodeuitableviewswiftuiimageview

提问by μ4ρκ05

So, I created a custom table view cell with a label on the left and a UIImageView on the right. The label has a tag of 100 and the UIImageView a tag of 110.

因此,我创建了一个自定义表格视图单元格,左侧有一个标签,右侧有一个 UIImageView。标签的标签为 100,而 UIImageView 的标签为 110。

My code is the following:

我的代码如下:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("ThemeCell") as UITableViewCell

    let theme = themes[indexPath.row]

    var themeName = cell.viewWithTag(100) as? UILabel
    themeName?.text = theme.themeName

    let themeImage = cell.viewWithTag(110) as? UIImageView
    themeImage?.image = UIImage(named: "test.png")

    //1
    //cell.imageView?.image = UIImage(named: "test.png")

    println("The loaded image: \(themeImage?.image)")

    return cell;
}

As is is, the themeName is displayed but the themeImage does not appear, although from println it seems that the image is loaded. If I uncomment the code in 1 the image appears in the custom cell but of course does not appear in the proper place as it is not added to the UIImageView that I created in IB. Any ideas what I might be doing wrong? The Tags are all correct. Thanks

按原样显示,themeName 已显示,但themeImage 未出现,尽管从 println 看来图像已加载。如果我取消注释 1 中的代码,图像会出现在自定义单元格中,但当然不会出现在正确的位置,因为它没有添加到我在 IB 中创建的 UIImageView。任何想法我可能做错了什么?标签都是正确的。谢谢

回答by imike

Firstly, you need to pin your views with auto layout mechanism. Open interface builder, left click on label inside your custom cell, then for example do the following:

首先,您需要使用自动布局机制固定您的视图。打开界面生成器,左键单击自定义单元格内的标签,然后执行以下操作:

  1. Editor->Pin->Width
  2. Editor->Pin->Height
  3. Editor->Pin->Leading Space to Superview
  4. Editor->Pin->Top Space to Superview
  1. 编辑器->引脚->宽度
  2. 编辑器->引脚->高度
  3. Editor->Pin->Leading Space to Superview
  4. Editor->Pin->Top Space to Superview

the same for image inside your custom cell

自定义单元格内的图像相同

  1. Editor->Pin->Width
  2. Editor->Pin->Height
  3. Editor->Pin->Trailing Space to Superview
  4. Editor->Pin->Top Space to Superview
  1. 编辑器->引脚->宽度
  2. 编辑器->引脚->高度
  3. Editor->Pin->Trailing Space to Superview
  4. Editor->Pin->Top Space to Superview

Then create custom class for your cell. for example

然后为您的单元格创建自定义类。例如

MyCustomCell.swift

MyCustomCell.swift

import Foundation
import UIKit

class MyCustomCell: UITableViewCell {
    @IBOutlet weak var myLabel: UILabel!
    @IBOutlet weak var myImageView: UIImageView!
}

Then set custom class for your cell and create connections from elements.

然后为您的单元格设置自定义类并从元素创建连接。

And now in tableViewController you can set the values to your elements without tags:

现在在 tableViewController 中,您可以将值设置为没有标签的元素:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell: MyCustomCell = tableView.dequeueReusableCellWithIdentifier("ThemeCell") as MyCustomCell

    let theme = themes[indexPath.row]

    cell.myLabel.text = theme.themeName
    cell.myImageView.image = UIImage(named: "test.png")

    println("The loaded image: \(cell.myImageView.image)")

    return cell;
}

回答by μ4ρκ05

Ok, so the fault was not in the UITable at all but in the fact that the AutoLayout was not set correctly and the image appeared outside the tableview...

好的,所以错误根本不在 UITable 中,而是因为 AutoLayout 设置不正确并且图像出现在 tableview 之外......