Я застрял в этой проблеме весь день и до сих пор не знаю, почему эту проблему так сложно понять. Я пытаюсь получить простой UITableview для работы. Мне удалось заставить его появиться на экране (слезы печали), но я не могу заставить табличное представление обновляться или вообще что-либо делать.UITableView в UIView с быстрым
Важно отметить, что мой UITableView находится внутри класса UIView, который я вызываю в UIViewController.
Вот мой UIView класс:
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(), вызывается, но ничего не помещается в таблицу. Плюс, когда я вызываю reloadData(), я никогда не вижу «таблица перезагрузилась ... \ n», напечатанная на экране. Это означает, что в таблицу никогда не добавляются ячейки.
Примечание: Я получил эту работу в объективе-c, но быстрый, по-видимому, немного сложнее для меня. И я знаю, что кто-то скажет мне, что я должен иметь UITableViewController, но таблица должна быть в отдельном UIView для моего приложения, спасибо!
Проверьте размер вашего Tableview, это возможно ноль. – Bannings
Отметьте мой ответ plz – Bannings