MyTapCell1.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
其All DataSource和Delegate设置正确。但是我的自定义单元格没有显示。
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
}
}
我的工作代码链接:自定义表
您应该为单元格注册class
。为此将这行代码更改为
self.tableView.registerClass(TapCell1.classForCoder(), forCellReuseIdentifier: "Cell")
编辑
你的代码看起来很好,我检查了
//cell.labelText.text="Cell Text"
cell.textLabel.text="Cell Text" // use like this
解决方案最有可能是手动设置单元格高度,如下所示:
override func tableView(tableView:UITableView!, heightForRowAtIndexPath indexPath:NSIndexPath)->CGFloat
{
return 44
}
我相信这是一个Xcode测试版6的错误。
我现在已经能够让Custom UITableViewCell工作了。
正在工作
- 在Xcode 6测试版6上运行
-
在Xcode 6 beta 5 上运行
-
iOS是7.1
如何
- 我已经为单元格创建了一个".xib"文件
- 我把它复制到故事板上
- 通过故事板,我给它一个标识符
- 我确保它是表视图的子级
这样做,你不需要注册一个类/nib等
这是我的自定义单元格。
导入UIKit类TestCell:UITableViewCell{@IBOutlet var titleImageView:UIImageView!@IBOutlet var title标签:UILabel!override init(style:UITableViewCellStyle,reuseIdentifier:String!({super.init(style:style,reuseIdentifier:reuseIdentifier(}重写函数wakeFromNib(({super.weakeFromNib((//初始化代码}必需的init(编码器aDecoder:NSCoder({super.init(编码器:aDecoder(}}
在您的视图中,或者在您扩展"UITableViewDataSource"的任何位置。确保"cell2"与您通过故事板提供的"标识符"相同。
func tableView(tableView:UITableView!,cellForRowAtIndexPath indexPath:NSIndexPath!(->UITableViewCell!{var cell:TestCell=tableView.dequeueReusableCellWithIdentifier("cell2",forIndexPath:indexPath(作为TestCell//使用自定义元素的示例。cell.titleLabel.text=self.items[indexPath.row]var topImage=UIImage(命名为:"fv.png"(cell.titleImageView.image=顶部图像返回单元格}
uitableviewcell
检查您的故事板,选择单元格,并查看"身份检查器",在其中选择class键入您的CustomClass,MODULE键入您的项目名称
我已经做到了这一点。它的工作原理非常完美。尝试这个技巧以避免错误,看到下面的图像和代码
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
}
尝试以下代码
var cell:CustomTableViewCell = tableView.dequeueReusableCellWithIdentifier("CustomTableViewCell") as CustomTableViewCell
https://github.com/iappvk/TableView-Swift
如果您不使用Storyboard,则创建一个新文件作为UITableViewCell的子类,在设置自定义单元格后选中复选框"也创建xib文件"。以下是表视图的代码
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!
}
尝试以下代码:
var cell = tableView.dequeueReusableCell(withIdentifier: "CustomCellTableView") as? CustomCellTableView
NSOject的扩展
extension NSObject {
var name: String {
return String(describing: type(of: self))
}
class var name: String {
return String(describing: self)
}
}
自定义TableViewCell的属性
class var nib: UINib {
return UINib(nibName:YourTableViewCell.name, bundle: nil)
}
class var idetifier: String {
return YourTableViewCell.name
}
加载项UIViewController
self.tableView.registerNib(YourTableViewCell.nib, forCellReuseIdentifier: YourTableViewCell.idetifier)
let cell = tableView.dequeueReusableCellWithIdentifier(YourTableViewCell.idetifier, forIndexPath: indexPath) as! YourTableViewCell
这是一种纯粹的快速表示法,适用于我的
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!
}