[英]UITableView inside a ViewController with custom cell without storyboard
I'm working with Swift 2.0 and Xcode 7.2.我正在使用 Swift 2.0 和 Xcode 7.2。
I want to learn how to make an app without a storyboard (UI with pure programming code).我想学习如何制作没有故事板的应用程序(使用纯编程代码的 UI)。 To start off, I am trying to make a simple app, with three labels, inside a custom UITableView cell which will be updated dynamically through the internet.
首先,我尝试在自定义 UITableView 单元格内制作一个带有三个标签的简单应用程序,该单元格将通过 Internet 动态更新。
Here is what I have achieved so far:这是我到目前为止所取得的成就:
rootViewController
in AppDelegate
AppDelegate
中添加了一个视图控制器作为rootViewController
UITableView
inside this viewUITableView
的代码Here are the other tasks I want to accomplish (all programmatically, without using the attribute inspector):这是我想要完成的其他任务(全部以编程方式,不使用属性检查器):
UINavigationController
into the ViewController
UINavigationController
插入ViewController
If possible, I would want to have the ability to have everything working in landscape mode as well.如果可能的话,我希望能够让所有东西都在横向模式下工作。
Can anyone tell me how to do this?谁能告诉我该怎么做?
AppDelegate.swift AppDelegate.swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window!.backgroundColor = UIColor.whiteColor()
window!.rootViewController = ViewController()
window!.makeKeyAndVisible()
return true
}
ViewController.swift视图控制器.swift
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView = UITableView(frame: self.view.bounds, style: UITableViewStyle.Plain)
tableView.dataSource = self
tableView.delegate = self
tableView.backgroundColor = UIColor.whiteColor()
tableView.frame = CGRectMake(0 , 0, self.view.bounds.width, self.view.bounds.height)//Optional for table size
self.view.addSubview(tableView)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let myCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "myIdentifier")
myCell.textLabel?.text = "\(indexPath.row)"
myCell.detailTextLabel?.text = "Subtitle"
return myCell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I have no idea how to create a custom cell programmatically to which I can add objects.我不知道如何以编程方式创建可以向其添加对象的自定义单元格。
Help would be appreciated.帮助将不胜感激。
Thanks.谢谢。
If you are not using storyboard, you can define your cell just above the class where your ViewController
where your are including your tableView
something like myCell which is your custom UITableViewCell
as given below.如果您不使用情节提要,则可以在您的
ViewController
所在的类上方定义您的单元格,其中包含您的tableView
,例如 myCell,它是您的自定义UITableViewCell
,如下所示。
In this myCell, you can add as many objects as your want and set them up in the setUpCell()
block.在这个 myCell 中,您可以添加任意数量的对象,并在
setUpCell()
块中设置它们。
The full code is as below, please make sure you call setUpCell()
when you use your cell's in cellForRowAtIndexPath
.完整代码如下,请确保在
cellForRowAtIndexPath
中使用单元格时调用setUpCell()
。
ViewController.swift视图控制器.swift
import #UIKit
class myCell: UITableViewCell {
// Define label, textField etc
var aMap: UILabel!
// Setup your objects
func setUpCell() {
aMap = UILabel(frame: CGRectMake(0, 0, 200, 50))
self.contentView.addSubview(aMap)
}
}
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableView = UITableView()
// for ex, lets say, your data array is defined in the variable below
var dataArray = [[String:AnyObject]]() //Array of your data to be displayed
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView = UITableView(frame: self.view.bounds, style: UITableViewStyle.Plain)
tableView.dataSource = self
tableView.delegate = self
tableView.backgroundColor = UIColor.whiteColor()
// register your class with cell identifier
self.tableView.registerClass(myCell.self as AnyClass, forCellReuseIdentifier: "Cell")
self.view.addSubview(tableView)
dataArray = // Something loaded from internet
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return flightDataArr.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// let myCell = tableView.dequeueReusableCellWithIdentifier("myIdentifier", forIndexPath: indexPath)
var cell:myCell? = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as? myCell
if cell == nil {
cell = myCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
}
var data = dataArray[indexPath.row]
cell?.setUpCell()
cell!.aMap.text = String(dict["productName"])
return cell!
}
}
See if this works for you.看看这是否适合你。 I never used programming to create
tableView
, so this may not be the optimal way to create your tableView
programmatically.我从未使用编程来创建
tableView
,因此这可能不是以编程方式创建tableView
的最佳方式。 I hope someone else may help you with a better answer if possible.如果可能的话,我希望其他人可以帮助您提供更好的答案。
You can create a sub class of UITableViewCell say PackageListTableViewCell.您可以创建 UITableViewCell 的子类,例如 PackageListTableViewCell。
Declare number of labels in tabelViewCell custom class as per your requirements like below,根据您的要求在 tabelViewCell 自定义类中声明标签数量,如下所示,
var label1 : UILabel?;
override init:reuseIdentifier:
in custom cell with additional parameters as below.在自定义单元格中覆盖
init:reuseIdentifier:
附加参数,如下所示。
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
//create labels as per your requirement
self.label1 = //initialise you label
//set frame, or constraint
//set text color, background color etc
//add created labels to cell as below
self.contentView.addSubView(self.label1);
}
your tableView:cellForRowAtIndexPath:
will be look like,你的
tableView:cellForRowAtIndexPath:
看起来像,
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let lable1String = "lbl1"
let lable2String = "lbl2"
let lable3String = "lbl3"
var cell : PackageListTableViewCell! = tableView.dequeueReusableCellWithIdentifier("cellID") as?PackageListTableViewCell
if (cell == nil) {
cell = PackageListTableViewCell.init(style: UITableViewCellStyle.Default,
reuseIdentifier:"cellID");
}
cell.selectionStyle = UITableViewCellSelectionStyle.None;
//set text of your lables as below
cell.label1.text = lable1String;
return cell;
}
You have to register a custom tableviewcell class using method registerClass on tableview.您必须使用 tableview 上的 registerClass 方法注册自定义 tableviewcell 类。
Use this modified Viewcontroller code:使用这个修改后的 Viewcontroller 代码:
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableView = UITableView(frame: self.view.bounds, style: UITableViewStyle.Plain)
tableView.dataSource = self
tableView.delegate = self
tableView.backgroundColor = UIColor.whiteColor()
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "myIdentifier")
tableView.frame = CGRectMake(0 , 0, self.view.bounds.width, self.view.bounds.height)//Optional for table size
self.view.addSubview(tableView)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let myCell = tableView.dequeueReusableCellWithIdentifier("myIdentifier", forIndexPath: indexPath)
myCell.textLabel?.text = "\(indexPath.row)"
myCell.detailTextLabel?.text = "Subtitle"
return myCell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.