ios 使用 swift 以编程方式 UITableView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24023613/
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
Programmatically UITableView using swift
提问by Sebastian
I'm trying to create a simple tableView programmatically using swift, so I wrote this code on "AppDelegate.swift" :
我正在尝试使用 swift 以编程方式创建一个简单的 tableView,所以我在 "AppDelegate.swift" 上写了这段代码:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
var tvc :TableViewController = TableViewController(style: UITableViewStyle.Plain)
self.window!.rootViewController = tvc
self.window!.backgroundColor = UIColor.whiteColor()
self.window!.makeKeyAndVisible()
return true
}
Basically I added the TableViewController creation and added to the window. And this is the TableViewController code:
基本上我添加了 TableViewController 创建并添加到窗口中。这是 TableViewController 代码:
class TableViewController: UITableViewController {
init(style: UITableViewStyle) {
super.init(style: style)
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// #pragma mark - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
return 1
}
override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
return 10
}
override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as UITableViewCell
cell.textLabel.text = "Hello World"
return cell
}
}
}
So, when I try to run the code I receive this message:
因此,当我尝试运行代码时,我收到此消息:
Xcode6Projects/TableSwift/TableSwift/TableViewController.swift: 12: 12: fatal error: use of unimplemented initializer 'init(nibName:bundle:)' for class 'TableSwift.TableViewController'
Xcode6Projects/TableSwift/TableSwift/TableViewController.swift: 12: 12: 致命错误:使用未实现的初始化程序 'init(nibName:bundle:)' 用于类 'TableSwift.TableViewController'
The error occurs when the compiler is executing the
编译器执行时出现错误
super.init(style: style)
super.init(风格:风格)
Any thoughts ?
有什么想法吗 ?
采纳答案by Sulthan
In Xcode 6 Beta 4
在 Xcode 6 Beta 4 中
Removing
删除
init(style: UITableViewStyle) {
super.init(style: style)
}
will do the trick. This is caused by different initializer behavior between Obj-C and Swift. You have created a designated initializer. If you remove it, all initializers will be inherited.
会做的伎俩。这是由 Obj-C 和 Swift 之间不同的初始化行为引起的。您已经创建了一个指定的初始化程序。如果删除它,所有初始化器都将被继承。
The root cause is probably in -[UITableViewController initWithStyle:]
which calls
根本原因可能是-[UITableViewController initWithStyle:]
调用
[self initWithNibName:bundle:]
I actually think this might be a bug in the way Obj-C classes are converted to Swift classes.
我实际上认为这可能是 Obj-C 类转换为 Swift 类的方式中的一个错误。
回答by ma11hew28
Instead of
代替
init(style: UITableViewStyle) {
super.init(style: style)
}
you might find this handy:
你可能会发现这很方便:
convenience init() {
self.init(style: .Plain)
title = "Plain Table"
}
Then, you can just call TableViewController()
to initialize.
然后,您只需调用TableViewController()
即可进行初始化。
回答by ANIL.MUNDURU
It is as simple as writing a function
就像写一个函数一样简单
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
{
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")
cell.text = self.Myarray[indexPath.row]
cell.textLabel.textColor = UIColor.greenColor()
cell.detailTextLabel.text = "DummyData #\(indexPath.row)"
cell.detailTextLabel.textColor = UIColor.redColor()
cell.imageView.image = UIImage(named:"123.png")
return cell
}
回答by ANIL.MUNDURU
Cell Function Use:
细胞功能用途:
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!{
var cell = tableView.dequeueReusableCellWithIdentifier(kLCellIdentifier) as UITableViewCell!
if !cell {
cell = UITableViewCell(style:.Default, reuseIdentifier: kLCellIdentifier)
}
cell.backgroundColor = UIColor.clearColor()
cell.textLabel.text = arrData[indexPath.row]
cell.image = UIImage(named: "\(arrImage[indexPath.row])")
cell.accessoryType = UITableViewCellAccessoryType.DetailDisclosureButton
cell.selectionStyle = UITableViewCellSelectionStyle.None
return cell
}