在swift中创建自定义tableview单元格

在swift中创建自定义tableview单元

我有一个带有几个IBOutlets的自定义单元类。我已将该课程添加到故事板中。我连接了所有的插座。我的cellForRowAtIndexPath函数如下所示:

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as SwipeableCell

        cell.mainTextLabel.text = self.venueService.mainCategoriesArray()[indexPath.row]

        return cell    }

这是我的自定义单元类:

class SwipeableCell: UITableViewCell {
    @IBOutlet var option1: UIButton
    @IBOutlet var option2: UIButton
    @IBOutlet var topLayerView : UIView
    @IBOutlet var mainTextLabel : UILabel
    @IBOutlet var categoryIcon : UIImageView

    init(style: UITableViewCellStyle, reuseIdentifier: String!) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)


    }}

当我运行应用程序时,我的所有单元格都是空的。我已经注销self.venueService.mainCategoriesArray(),它包含所有正确的字符串。我也试过把一个实际的字符串等于标签,这会产生相同的结果。

我错过了什么?任何帮助表示赞赏。


拉风的咖菲猫
浏览 1101回答 3
3回答

白衣非少年

这适用于使用.xib工作的自定义单元格func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{     let identifier = "Custom"     var cell: CustomCell! = tableView.dequeueReusableCellWithIdentifier(identifier) as? CustomCel         if cell == nil {             tableView.registerNib(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: identifier)            cell =tableView.dequeueReusableCellWithIdentifier(identifier) as? CustomCell         }return cell}

aluckdog

我也有同样的问题。一般来说,我做的和你一样。class dynamicCell: UITableViewCell {     @IBOutlet var testLabel : UILabel     init(style: UITableViewCellStyle, reuseIdentifier: String) {         super.init(style: style, reuseIdentifier: reuseIdentifier)     }     override func awakeFromNib() {         super.awakeFromNib()     }     override func setSelected(selected: Bool, animated: Bool) {         super.setSelected(selected, animated: animated)     }}并在uitableviewcell方法中:func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {     var cell :dynamicCell = tableView.dequeueReusableCellWithIdentifier("cell") as dynamicCell     cell.testLabel.text = "so sad"     println(cell.testLabel)     return cell;}是的,tableview什么都没显示!但是猜猜看,它实际上显示了一些东西......因为我从println(cell.testLabel)获得的日志显示所有标签实际上都显示出来了。但!他们的框架很奇怪,有这样的东西:frame =(0 -21; 42 21);所以它有一个(0,-21)为(x,y),这意味着标签只出现在单元格边界之外的某个地方。所以我尝试手动添加调整框架,如下所示:cell.testLabel.frame = CGRectMake(10,10,42,21)可悲的是,它不起作用。--------------- 10分钟后更新-----------------我做的。所以,似乎问题来自Size Classes。单击.storyboard文件,然后转到文件检查器选项卡取消勾选“大小类”复选框最后,我的“太悲伤”的标签出来了!
打开App,查看更多内容
随时随地看视频慕课网APP