μ4ρ*_*κ05 8 xcode uitableview uiimageview ios swift
因此,我创建了一个自定义表格视图单元格,左侧是标签,右侧是UIImageView.标签的标签为100,UIImageView的标签为110.
我的代码如下:
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;
}
Run Code Online (Sandbox Code Playgroud)
按原样,显示themeName但不显示themeImage,尽管从println看起来似乎已加载图像.如果我取消注释1中的代码,图像将出现在自定义单元格中,但当然不会出现在正确的位置,因为它没有添加到我在IB中创建的UIImageView中.我有什么想法可能做错了吗?标签都是正确的.谢谢
imi*_*ike 12
首先,您需要使用自动布局机制固定视图.打开界面构建器,左键单击自定义单元格内的标签,然后执行以下操作:
自定义单元格中的图像也是如此
然后为您的单元格创建自定义类.例如
MyCustomCell.swift
import Foundation
import UIKit
class MyCustomCell: UITableViewCell {
    @IBOutlet weak var myLabel: UILabel!
    @IBOutlet weak var myImageView: UIImageView!
}
Run Code Online (Sandbox Code Playgroud)
然后为您的单元格设置自定义类,并从元素创建连接.
现在在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;
}
Run Code Online (Sandbox Code Playgroud)
        |   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           14146 次  |  
        
|   最近记录:  |