Swift Custom Cell使用标签创建自己的Cell [英] Swift Custom Cell creating your own Cell with labels

查看:226
本文介绍了Swift Custom Cell使用标签创建自己的Cell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用Swift作为prorgamming语言,我遇到了自定义单元格的问题。



当我尝试创建自定义单元格时,然后继续前进并尝试按照我需要的方式设计它们(将Style设置为Custom)一切看起来都不错。现在我不知道如何将特定数据放入其中,因为我找到的所有教程都使用了样式选项basic,它们只有一个文本标签,用于分配数据。



现在对我来说,当我控制将我的标签拖到我的代码中时,我会给它们特定的名称,例如dateLabel或sourceLabel,以便正确插入数据。



现在我不确定,也找不到任何有用的答案,如何回忆我的自定义标签,以便我可以将数据分配给他们。 ..



也许有人可以帮我这个,因为我很确定这是一个简单的问题,但我找不到任何资源给这个^^





希望字体不小,我只是想让你们看到我得到的错误。



我使用以下教程作为指导,因为它是唯一一个像这个人那样工作的人它:


  • 更新故事板的单元格并为其指定 重用身份 值。转到属性检查器并设置标识符值。例如,我给我的单元格标识符值 MyCustomCell


  • 控制将单元格的标签拖动到新的自定义表格视图单元格类中(即, MyCustomTableViewCell 类。)







  • 完成上述步骤后,当 tableView中出列您的单元格时,您将能够访问标签:cellForRowAtIndexPath:方法。如下面的代码片段所示,您需要:1)使用您在上述步骤中建立的重用标识符获取单元格,并且2)转换为自定义表格视图单元格类。



    例如,如果你将它命名为 MyCustomTableViewCell ,这就是你的自定义表视图单元格的样子。这是在您创建类和控件后将标签拖到此类中。

      class MyCustomTableViewCell:UITableViewCell {
    @ IBOutlet弱变类别标签:UILabel!
    @IBOutlet weak var dateLabel:UILabel!
    @IBOutlet weak var sourceLabel:UILabel!
    @IBOutlet weak var titleLabel:UILabel!
    }

    您的 ViewController 可以看起来像这样:

      //注意:我将UITableViewController子类化,因为它提供了
    //委托和数据源协议。考虑这样做。
    class ViewController:UITableViewController {

    //你不需要你的UILabel,因为他们移动到你的
    //自定义单元类。

    // ... ...
    //为简洁起见,在此代码段中省略其他方法。
    // ...

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

    //使用单元格的重用标识符并将结果
    //强制转换为自定义表格单元格类。
    让article = tableView.dequeueReusableCellWithIdentifier(MyCustomCell,forIndexPath:indexPath)为! MyCustomTableViewCell

    //你应该有权访问你的标签;分配值。
    article.categoryLabel?.text =something
    article.dateLabel?.text =something
    article.sourceLabel?.text =something
    article.titleLabel? .text =something

    返回文章
    }
    }


    I've just started to use Swift as a prorgamming language and i've run into a problem with Custom cells.

    When i try to create custom cells, and then go forward and try to design them the way i need them ( with Style set to Custom ) everything looks good. Now i don't know how to put specific data into them, since all tutorials i found used the style option "basic" where they only have a text label to which they assign their data.

    Now for me, when i "control drag" my labels into my code, i give them specific names such as "dateLabel" or "sourceLabel" in order to insert the data correctly.

    now i'm not sure, and couldn't find any answers that worked, on how to recall my custom made labels so that i can assign my data to them...

    Maybe someone of you could help me with this, since i'm pretty sure it's a simple problem but i coudln't find any resources to this ^^

    hopefully the font isn't to small, i just wanted you guys to see the erros i get.

    I used the following tutorial as a guide line, since it was the only one that worked just the way this guy did it: https://www.youtube.com/watch?v=0qE8olxB3Kk

    I checked the identifier and he is set correctly and i can't find anything online on how i have to properly refer to my own labels with their correct names.

    any help would be appreciated :)

    解决方案

    Try the following steps:

    1. Create a custom table view cell class that extends UITableViewCell. In my example, the custom table view cell class is called MyCustomTableViewCell.

    2. Update your storyboard's cell so that it uses your custom table view cell class. Go to the Identity Inspector and set the Class value to the name of your custom table view cell class.

    3. Update your storyboard's cell and give it a reuse identity value. Go to the Attributes Inspector and set the Identifier value. For example, I gave my cell an Identifier value of MyCustomCell.

    4. Control drag the cell's labels into your new custom table view cell class (i.e., the MyCustomTableViewCell class).


    Once you have done the above steps, you will be able to access the labels when you dequeue your cell in the tableView:cellForRowAtIndexPath: method. As the code snippet below shows, you will need to: 1) get the cell using the reuse identifier you established in the steps above and 2) cast to your custom table view cell class.

    For example, here's what your custom table view cell would look like if you named it MyCustomTableViewCell. This is after you created the class and control dragged your labels into this class.

    class MyCustomTableViewCell: UITableViewCell {    
        @IBOutlet weak var categoryLabel: UILabel!
        @IBOutlet weak var dateLabel: UILabel!
        @IBOutlet weak var sourceLabel: UILabel!
        @IBOutlet weak var titleLabel: UILabel!
    } 
    

    Your ViewController could look like this:

    // NOTE: I subclassed UITableViewController since it provides the
    // delegate and data source protocols. Consider doing this.
    class ViewController: UITableViewController {
    
        // You do NOT need your UILabels since they moved to your
        // custom cell class.
    
        // ...
        // Omitting your other methods in this code snippet for brevity.
        // ... 
    
        override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
            // Use your cell's reuse identifier and cast the result
            // to your custom table cell class. 
            let article = tableView.dequeueReusableCellWithIdentifier("MyCustomCell", forIndexPath: indexPath) as! MyCustomTableViewCell
    
            // You should have access to your labels; assign the values.
            article.categoryLabel?.text = "something"            
            article.dateLabel?.text = "something"            
            article.sourceLabel?.text = "something"            
            article.titleLabel?.text = "something"            
    
            return article
        }
    }
    

    这篇关于Swift Custom Cell使用标签创建自己的Cell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆