Ios 使用swift在UIViewController中设置编程生成的UITableView的UITableViewCells
好的,我的目标是将自定义单元格绘制到UIViewController中以编程方式生成的UITableView中。我有一个名为KillFeedTableViewCell的自定义单元格类,它也有一个.xib 我在xib中确保了单元格标识符是KillFeedTableViewCell,这也是我在下面的代码中用作标识符的内容 我遇到的问题是它根本无法运行。当我运行时没有错误,当我运行时tableView看起来很完美,只是没有单元格。我还有几行代码可以将文本打印到控制台,以确保tableView函数正在运行,而不是 非常感谢您的帮助,谢谢 在FirstViewController的开头Ios 使用swift在UIViewController中设置编程生成的UITableView的UITableViewCells,ios,iphone,uitableview,swift,Ios,Iphone,Uitableview,Swift,好的,我的目标是将自定义单元格绘制到UIViewController中以编程方式生成的UITableView中。我有一个名为KillFeedTableViewCell的自定义单元格类,它也有一个.xib 我在xib中确保了单元格标识符是KillFeedTableViewCell,这也是我在下面的代码中用作标识符的内容 我遇到的问题是它根本无法运行。当我运行时没有错误,当我运行时tableView看起来很完美,只是没有单元格。我还有几行代码可以将文本打印到控制台,以确保tableView函数正在运
class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{
//code
var tableView:UITableView = UITableView()
//code
}
稍后在文件中,我像这样设置了tableView
var fullHeight:CGFloat = self.view.bounds.size.height-50
tableView.frame = CGRectMake(0, fullHeight, self.view.bounds.size.width, 0)
// height is zero because you drag the tableView up filling the screen
tableView.alpha = 0.5
self.view.addSubview(tableView)
最后,在FirstViewController的末尾,在关闭类的括号之前,我有必要的tableView函数和一些
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var tempData = serverAPIHandler.getKillCount(username)
var tempData2 = JSONSwift(tempData)
println("table view kill count") // this is here so i know the code runs but this never shows up in the log
return tempData2["kill_count"].intValue
}
func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: KillFeedTableViewCell = tableView.dequeueReusableCellWithIdentifier("KillFeedTableViewCell") as KillFeedTableViewCell
println(cell) // this is here so i know the code runs but this never shows up in the log
return cell;
}
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
println("Row \(indexPath.row) selected")
}
您必须记住注册通常在viewDidLoad中完成的Nib文件
UINib *cellNib = [UINib nibWithNibName:@"cell" bundle:nil];
[self.tableView registerNib:cellNib forCellReuseIdentifier:@"cell"];
看起来您没有设置表视图的委托或数据源…@mattt haha我该怎么做?我是新来的。。。我17岁,自学成才,这款应用程序将用于明年的高级刺客游戏。。当我是高级代理时。委托是TableView上的一个属性,请将其设置为等于self。您还应该声明自己符合UITableViewDelegate协议。