ios 使用 Swift 的 UITableView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24056183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:30:06  来源:igfitidea点击:

UITableView Using Swift

iosuitableviewswiftcustomization

提问by PREMKUMAR

My TapCell1.swift

我的TapCell1.swift

This is Custom UITableViewCell View

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

我的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

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

回答by PREMKUMAR

I finally did it.

我终于做到了。

For TapCell1.swift

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

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

我的工作代码链接:CUSTOMIZED TABLE

回答by Anil Varghese

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

您应该class为单元注册。为此,请将此行代码更改为

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

回答by Micah

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.

我相信这是一个 Xcode beta 6 错误。

回答by thefreshteapot

I have now been able to get Custom UITableViewCell to work.

我现在已经能够让自定义 UITableViewCell 工作。

Works on

适用于

  • Runs on Xcode 6 beta 6
  • Runs on Xcode 6 beta 5

  • iOS is 7.1

  • 在 Xcode 6 beta 6 上运行
  • 在 Xcode 6 beta 5 上运行

  • iOS是7.1

How

如何

  • I have created a ".xib" file for the cell.
  • I copy it into the storyboard.
  • Via the storyboard I give it a Identifier.
  • I make sure it is a sub child of a tableview
  • 我为单元创建了一个“.xib”文件。
  • 我将其复制到故事板中。
  • 通过故事板,我给它一个标识符。
  • 我确保它是 tableview 的子子项

Doing it this way, you do not need to register a class / nib etc.

这样做,您不需要注册类/笔尖等。

This is my custom cell.

这是我的自定义单元格。

import UIKit

class TestCell: UITableViewCell {

    @IBOutlet var titleImageView: UIImageView!
    @IBOutlet var titleLabel: UILabel!

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

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

In your view, or where ever you extend "UITableViewDataSource". Make sure "cell2" is the same as the "Identifier" that you gave it via the storyboard.

在您看来,或在您扩展“UITableViewDataSource”的任何地方。确保“cell2”与您通过故事板提供的“标识符”相同。

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

    var cell:TestCell = tableView.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) as TestCell

    // Example of using the custom elements.
    cell.titleLabel.text = self.items[indexPath.row]

    var topImage = UIImage(named: "fv.png")
    cell.titleImageView.image = topImage

    return cell
}

uitableviewcell

合适的视图单元

回答by guru

If you are not using Storyboard then create a new file as subclass of UITableViewCell check the checkbox "also create xib files" after that set your custom cell .and here is the code for tableview

如果您不使用 Storyboard,则创建一个新文件作为 UITableViewCell 的子类,然后在设置自定义单元格后选中“也创建 xib 文件”复选框。这是 tableview 的代码

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

    var customcell:CustomTableViewCellClass? = tableView.dequeueReusableCellWithIdentifier("cell") as? CustomTableViewCellClass
    if  (customcell==nil)
    {
        var nib:NSArray=NSBundle.mainBundle().loadNibNamed("CustomTableViewCellClass", owner: self, options: nil)

        customcell = nib.objectAtIndex(0) as? CustomTableViewCell
    }

    return customcell!


}

回答by Vijay

Try this following code

试试这个下面的代码

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

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

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

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

回答by Quad

Check your Story board select the cell and look at the "identity inspector in that select CLASS type your CustomClass and MODULE type your project name

检查您的故事板,选择单元格并查看“身份检查器”中的选择 CLASS 键入您的 CustomClass 和 MODULE 键入您的项目名称

I have done this It works perfectly try this tip to avoid error to see below image and code

我已经这样做了它完美地工作尝试这个提示以避免错误看到下面的图像和代码

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {

 // let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: nil)

  //  cell.textLabel.text = array[indexPath!.row] as String

let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as CustomTableViewCell

    cell.nameLabel.text = array[indexPath!.row] as String
    cell.RestaurentView.image = UIImage(named:images[indexPath!.row])

    return cell
}

回答by Soumya Soni

Try this following code:

试试这个下面的代码:

var cell = tableView.dequeueReusableCell(withIdentifier: "CustomCellTableView") as? CustomCellTableView

回答by Rakesh Kunwar

Extention of NSOject

NSOject 的扩展

extension NSObject {    
    var name: String {
        return String(describing: type(of: self))
    } 
    class var name: String {
        return String(describing: self)
    }
}

Properties on Custom TableViewCell

自定义 TableViewCell 上的属性

class var nib: UINib {
    return UINib(nibName:YourTableViewCell.name, bundle: nil)
}
class var idetifier: String {
    return YourTableViewCell.name
}

Add in UIViewController

在 UIViewController 中添加

self.tableView.registerNib(YourTableViewCell.nib, forCellReuseIdentifier: YourTableViewCell.idetifier)

let cell = tableView.dequeueReusableCellWithIdentifier(YourTableViewCell.idetifier, forIndexPath: indexPath) as! YourTableViewCell

回答by Gaurav

It is Purely swift notation an working for me

这是纯粹的快速符号对我有用

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
{ 
var cellIdentifier:String = "CustomFields" 
var cell:CustomCell? = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? CustomCell
 if (cell == nil) 
{ 
var nib:Array = NSBundle.mainBundle().loadNibNamed("CustomCell", owner: self, options: nil) cell = nib[0] as? CustomCell
 } 
return cell! 
}