[英]Creating a UITableViewCell programmatically in Swift
我正在嘗試為我的UITableView
創建一個自定義單元格,但我遇到了一些困難。
首先我不能使用Interface Builder,因為我在Xcode中遇到了這個bug的變種 。 每次我在Interface Builder中單擊一個元素時,該視圖中的所有內容都會獲得零的高度和寬度,並在視圖外部重新定位。 此外,我想學習如何以編程方式執行此操作。
其次,我正在為我的項目使用Swift語言。 我一直在嘗試跟隨這個演示 ,並盡力將Objective C代碼轉換為Swift,但每當遇到問題時,我最終都會陷入困境。 我認為這是因為我沒有正確地轉換代碼。
第三, 我發現了這個視頻但是盡管很難遵循(許多代碼只是被復制和粘貼而沒有太多解釋它的作用或原因),它仍然最終使用Interface Builder來改變各個部分。
我有一個基本的UITableView
設置很好。 我只是希望能夠在該表視圖中添加自定義單元格。
這可以使用純編程完成,還是需要使用Interface Builder?
任何人都能指出我正確的方向或幫助我在Swift中以編程方式創建自定義單元格嗎?
非常感謝。
一般來說:在純編程中一切皆有可能;-)
為tableView單元格創建自定義類,並設置所有元素,屬性和可視布局。 實現所需的方法init(style,reuseidentifier)
在UITableViewController
的自定義類中,使用registerClass(forCellReuseIdentifier)
注冊自定義單元類
為自定義tableViewController設置委托和數據源
最后,在cellForRowAtIndexPath
創建單元格:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("myReuseIdentifier", forIndexPath: indexPath) as MyCustomTableViewCell
// configure the cell using its properties
return cell
}
這應該是基本步驟。
如果您正在尋找更多代碼,以下是我創建的自定義單元格的示例:
// File: vDataEntryCell.swift
import UIKit
class vDataEntryCell: UITableViewCell
{
//-----------------
// MARK: PROPERTIES
//-----------------
//Locals
var textField : UITextField = UITextField()
//-----------------
// MARK: VIEW FUNCTIONS
//-----------------
///------------
//Method: Init with Style
//Purpose:
//Notes: This will NOT get called unless you call "registerClass, forCellReuseIdentifier" on your tableview
///------------
override init(style: UITableViewCellStyle, reuseIdentifier: String!)
{
//First Call Super
super.init(style: style, reuseIdentifier: reuseIdentifier)
//Initialize Text Field
self.textField = UITextField(frame: CGRect(x: 119.00, y: 9, width: 216.00, height: 31.00));
//Add TextField to SubView
self.addSubview(self.textField)
}
///------------
//Method: Init with Coder
//Purpose:
//Notes: This function is apparently required; gets called by default if you don't call "registerClass, forCellReuseIdentifier" on your tableview
///------------
required init(coder aDecoder: NSCoder)
{
//Just Call Super
super.init(coder: aDecoder)
}
}
然后在我的UITableViewController類中,我執行了以下操作:
// File: vcESDEnterCityState.swift
import UIKit
class vcESDEnterCityState: UITableViewController
{
//-----------------
// MARK: VC FUNCTIONS
//-----------------
///------------
//Method: View Will Appear
//Purpose:
//Notes:
///------------
override func viewWillAppear(animated: Bool)
{
//First Call Super
super.viewWillAppear(animated)
//Register the Custom DataCell
tvCityStateForm.registerClass(vDataEntryCell.classForCoder(), forCellReuseIdentifier: "cell")
}
//-----------------
// MARK: UITABLEVIEW DELEGATES
//-----------------
///------------
//Method: Cell for Row at Index Path of TableView
//Purpose:
//Notes:
///------------
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
//Get Reference to Cell
var cell : vDataEntryCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as vDataEntryCell
//...Do Stuff
//Return Cell
return cell
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.