使用Swift的UITableView [英] UITableView Using Swift

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

问题描述

我的 TapCell1.swift

这是自定义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)
    }
}

我的 ViewController.swift

所有数据源和代理均已正确设置.但是我的自定义单元格未显示.

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.
        }
}

问题是未显示我的自定义单元格.请提出我做错的任何事情.

注意:这是我的代码我的文件下载链接

解决方案

我终于做到了.

对于TapCell1.swift

import UIKit

class TapCell1: UITableViewCell {

    @IBOutlet var labelTitle: UILabel

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

对于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 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

解决方案

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

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

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