UIView 中的 UITableView swift
UITableView in UIView with swift
我整天都被这个问题困住了,仍然不知道为什么这个问题这么难解决。我一直在尝试让一个简单的 UITableview 工作。我设法让它出现在屏幕上(悲伤的泪水),但我无法让 table 视图更新或做任何事情。
请务必注意,我的 UITableView 位于我在 UIViewController 中调用的 UIView class 内。
这是我的 UIView class:
import Foundation
import UIKit
class comebackViewController:UIView{
@IBOutlet var table: UITableView!
func setup(){
table = UITableView()
}
}
这是我的主要 UIViewController:
import UIKit
class ViewCont: UIViewController, UITableViewDataSource, UITableViewDelegate{
var tableViewCont:comebackViewController!
var ListArray:NSMutableArray = []
override func viewDidLoad() {
super.viewDidLoad()
ListArray.addObject("hello")
ListArray.addObject("goodbye")
print(ListArray.count)
tableViewCont = NSBundle.mainBundle().loadNibNamed("comebackTableView", owner: self, options: nil).last as! comebackViewController
tableViewCont.frame = CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.height)
tableViewCont.table = UITableView()
tableViewCont.setup()
tableViewCont.table.dataSource = self
tableViewCont.table.delegate = self
tableViewCont.table.registerClass(tableViewCellController.self, forCellReuseIdentifier: "cell")
//Some people say you have to call reloadData like this...
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.tableViewCont.table.reloadData()
//let indexPath = NSIndexPath(forRow: 0, inSection: 0)
//self.tableViewCont.table.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
})
tableViewCont.table.reloadData()
self.view.addSubview(tableViewCont)
}
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
print("did get count...\n")
return self.ListArray.count;
}
func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
print("did get height...\n")
return 44
}
func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
print("did get number of sections...\n")
return 2
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
print("table did reload...\n")
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! tableViewCellController
cell.random1.text = "\(ListArray.objectAtIndex(indexPath.row))"
cell.random2.text = "me"
/*
if let desc = self.items[indexPath.row]["description"] as? NSString {
cell.detailTextLabel.text = desc
}*/
return cell
}
}
当我尝试 运行 终端打印出此代码时
did get number of sections...
did get count...
did get height...
did get height...
did get count...
did get height...
did get height...
这意味着当我调用 reloadData() 函数时正在调用,但没有任何内容被放入 table。另外,当我调用 reloadData() 时,我从未看到 "table did reload...\n" 打印到屏幕上。这意味着从来没有任何单元格被添加到 table.
注意:我已经在 objective-c 中完成了这项工作,但 swift 对我来说似乎有点困难。我知道有人会告诉我我必须有一个 UITableViewController,但是 table 必须在我的应用程序的单独 UIView 中,谢谢!
出现此行为是因为 tableView 的大小为零:
tableViewCont = NSBundle.mainBundle().loadNibNamed("comebackTableView", owner: self, options: nil).last as! comebackViewController
tableViewCont.frame = CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.height)
tableViewCont.table = UITableView()
tableViewCont.setup() // you instantiate a new UITableView
你应该在实例化后设置它的框架:
tableViewCont = NSBundle.mainBundle().loadNibNamed("comebackTableView", owner: self, options: nil).last as! comebackViewController
tableViewCont.setup()
tableViewCont.frame = CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.height)
我整天都被这个问题困住了,仍然不知道为什么这个问题这么难解决。我一直在尝试让一个简单的 UITableview 工作。我设法让它出现在屏幕上(悲伤的泪水),但我无法让 table 视图更新或做任何事情。
请务必注意,我的 UITableView 位于我在 UIViewController 中调用的 UIView class 内。
这是我的 UIView class:
import Foundation
import UIKit
class comebackViewController:UIView{
@IBOutlet var table: UITableView!
func setup(){
table = UITableView()
}
}
这是我的主要 UIViewController:
import UIKit
class ViewCont: UIViewController, UITableViewDataSource, UITableViewDelegate{
var tableViewCont:comebackViewController!
var ListArray:NSMutableArray = []
override func viewDidLoad() {
super.viewDidLoad()
ListArray.addObject("hello")
ListArray.addObject("goodbye")
print(ListArray.count)
tableViewCont = NSBundle.mainBundle().loadNibNamed("comebackTableView", owner: self, options: nil).last as! comebackViewController
tableViewCont.frame = CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.height)
tableViewCont.table = UITableView()
tableViewCont.setup()
tableViewCont.table.dataSource = self
tableViewCont.table.delegate = self
tableViewCont.table.registerClass(tableViewCellController.self, forCellReuseIdentifier: "cell")
//Some people say you have to call reloadData like this...
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.tableViewCont.table.reloadData()
//let indexPath = NSIndexPath(forRow: 0, inSection: 0)
//self.tableViewCont.table.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
})
tableViewCont.table.reloadData()
self.view.addSubview(tableViewCont)
}
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
print("did get count...\n")
return self.ListArray.count;
}
func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
print("did get height...\n")
return 44
}
func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
print("did get number of sections...\n")
return 2
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
print("table did reload...\n")
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! tableViewCellController
cell.random1.text = "\(ListArray.objectAtIndex(indexPath.row))"
cell.random2.text = "me"
/*
if let desc = self.items[indexPath.row]["description"] as? NSString {
cell.detailTextLabel.text = desc
}*/
return cell
}
}
当我尝试 运行 终端打印出此代码时
did get number of sections...
did get count...
did get height...
did get height...
did get count...
did get height...
did get height...
这意味着当我调用 reloadData() 函数时正在调用,但没有任何内容被放入 table。另外,当我调用 reloadData() 时,我从未看到 "table did reload...\n" 打印到屏幕上。这意味着从来没有任何单元格被添加到 table.
注意:我已经在 objective-c 中完成了这项工作,但 swift 对我来说似乎有点困难。我知道有人会告诉我我必须有一个 UITableViewController,但是 table 必须在我的应用程序的单独 UIView 中,谢谢!
出现此行为是因为 tableView 的大小为零:
tableViewCont = NSBundle.mainBundle().loadNibNamed("comebackTableView", owner: self, options: nil).last as! comebackViewController
tableViewCont.frame = CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.height)
tableViewCont.table = UITableView()
tableViewCont.setup() // you instantiate a new UITableView
你应该在实例化后设置它的框架:
tableViewCont = NSBundle.mainBundle().loadNibNamed("comebackTableView", owner: self, options: nil).last as! comebackViewController
tableViewCont.setup()
tableViewCont.frame = CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.height)