为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

拉风的咖菲猫 2019-08-27 15:31:07
在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(),它包含所有正确的字符串。我也试过把一个实际的字符串等于标签,这会产生相同的结果。我错过了什么?任何帮助表示赞赏。
查看完整描述

3 回答

?
白衣非少年

TA贡献1155条经验 获得超0个赞

这适用于使用.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}


查看完整回答
反对 回复 2019-08-27
?
aluckdog

TA贡献1847条经验 获得超7个赞

我也有同样的问题。

一般来说,我做的和你一样。

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文件,然后转到文件检查器选项卡

取消勾选“大小类”复选框

最后,我的“太悲伤”的标签出来了!


查看完整回答
反对 回复 2019-08-27
  • 3 回答
  • 0 关注
  • 1105 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信