ios Swift - UIViewController 中的 UITableView,未调用 UITableView 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30576291/
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
Swift - UITableView inside UIViewController, UITableView functions are not called
提问by serdar aylanc
I inserted a tableview inside a UIViewController. But my code is not working. When I checked I found that none of the tableview functions are not called.
我在 UIViewController 中插入了一个 tableview。但是我的代码不起作用。当我检查时,我发现没有调用任何 tableview 函数。
class CustomViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var authorArticlesTableView: UITableView!
var authorid: Int!
var authorname: String!
var articles: [JSON]? = []
func loadArticles(){
let url = "http:here.com/" + String(authorid) + "/"
println(url)
Alamofire.request(.GET, url).responseJSON { (Request, response, json, error) -> Void in
if (json != nil){
var jsonObj = JSON(json!)
if let data = jsonObj["articles"].arrayValue as [JSON]?{
self.articles = data
self.authorArticlesTableView.reloadData()
}
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.loadArticles()
println("viewDidLoad")
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
println("numberOfRowsInSection")
return self.articles?.count ?? 0
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("CustomCell") as! CustomTableViewCell
cell.articles = self.articles?[indexPath.row]
println("cellForRowAtIndexPath")
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("WebSegue", sender: indexPath)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
Any solution for this?
有什么解决办法吗?
Thanks,
谢谢,
回答by Greg
You have to set your view controller as a table view delegate/datasource:
您必须将视图控制器设置为表视图委托/数据源:
add to the end of the viewDidLoad
:
添加到 的末尾viewDidLoad
:
authorArticlesTableView.delegate = self
authorArticlesTableView.dataSource = self
回答by Ashish Kakkad
Set table delegate
and dataSource
:
设置表delegate
和dataSource
:
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
回答by OriDev
Sometimes, if you forget to drap and drop UITableViewCell to UITableView. XCode don't understand TableView has Cell. By default, when you drap and drop UITableView into UIViewController. I see UITableView has Cell. But you need to drap and drop UITableViewCell into UITableView also.
有时,如果您忘记将 UITableViewCell 拖放到 UITableView。XCode 不明白 TableView 有 Cell。默认情况下,当您将 UITableView 拖放到 UIViewController 中时。我看到 UITableView 有 Cell。但是您还需要将 UITableViewCell 拖放到 UITableView 中。
It is work with me.
这是和我一起工作。
回答by giacoder
Makes sense to do it in your
在你的工作中这样做是有意义的
@IBOutlet weak var authorArticlesTableView: UITableView!
so it will become
所以它会变成
@IBOutlet weak var authorArticlesTableView: UITableView! {
didSet {
authorArticlesTableView.delegate = self;
authorArticlesTableView.dataSource = self;
}
}