UITableView Using Swift

2019-01-17 00:27发布

My TapCell1.swift

This is Custom UITableViewCell View

import UIKit

class TapCell1: UITableViewCell
{
    @IBOutlet var labelText : UILabel

    init(style: UITableViewCellStyle, reuseIdentifier: String!)
    {
        println("Ente")
        super.init(style: UITableViewCellStyle.Value1, reuseIdentifier: reuseIdentifier)
    }

    override func setSelected(selected: Bool, animated: Bool)
    {

        super.setSelected(selected, animated: animated)
    }
}

My ViewController.swift

Its All DataSource and Delegates are set correctly.But My custom Cell is not displaying.

import UIKit

class NextViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
    {
        @IBOutlet var label: UILabel

        @IBOutlet var tableView : UITableView

        var getvalue = NSString()

        override func viewDidLoad()
        {
            super.viewDidLoad()

            label.text="HELLO GOLD"

            println("hello : \(getvalue)")
            self.tableView.registerClass(TapCell1.self, forCellReuseIdentifier: "Cell")
        }

        func tableView(tableView:UITableView!, numberOfRowsInSection section:Int)->Int
        {
            return 5
        }

        func numberOfSectionsInTableView(tableView:UITableView!)->Int
        {
            return 1
        }

        func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
        {
            var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as TapCell1
            cell.labelText.text="Cell Text"
            return cell
        }

        override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
        }
}

The Problem is My custom cell is Not displayed. Please suggest anything i did wrong.

Note: here is My code My File Download Link

9条回答
Bombasti
2楼-- · 2019-01-17 01:29

You should register the class for the cell. For that do change this line of code to

self.tableView.registerClass(TapCell1.classForCoder(), forCellReuseIdentifier: "Cell")

Edit

You code is looks fine i checked it

//cell.labelText.text="Cell Text"

cell.textLabel.text="Cell Text"   // use like this
查看更多
爷的心禁止访问
3楼-- · 2019-01-17 01:32

Try this following code

var cell:CustomTableViewCell = tableView.dequeueReusableCellWithIdentifier("CustomTableViewCell") as CustomTableViewCell

https://github.com/iappvk/TableView-Swift

查看更多
爷、活的狠高调
4楼-- · 2019-01-17 01:33

The solution is most likely pinpointed to setting the cell height manually as such:

override func tableView(tableView:UITableView!, heightForRowAtIndexPath indexPath:NSIndexPath)->CGFloat
{
    return 44
}

I believe it's an Xcode beta 6 bug.

查看更多
登录 后发表回答