Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
186 views
in Technique[技术] by (71.8m points)

ios - UITableView Using Swift

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I finally did it.

For TapCell1.swift

import UIKit

class TapCell1: UITableViewCell {

    @IBOutlet var labelTitle: UILabel

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

For NextViewController.swift

import UIKit

class NextViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var tableView: UITableView

    var ListArray=NSMutableArray()

    override func viewDidLoad() {
        super.viewDidLoad()

        let nibName = UINib(nibName: "TapCell1", bundle:nil)
        self.tableView.registerNib(nibName, forCellReuseIdentifier: "Cell")

        for i in 0...70 {
            ListArray .addObject("Content: (i)")
        }
    }


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

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

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

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

        //cell.titleLabel.text = "(ListArray.objectAtIndex(indexPath.item))"

        cell.labelTitle.text = "(ListArray.objectAtIndex(indexPath.row))"

        return cell
    }
}

My working code link: CUSTOMISED TABLE


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...