我想要一个UITableView使用的带subtitle式单元格dequeueReusableCellWithIdentifier.
我原来的Objective-C代码是:
static NSString* reuseIdentifier = @"Cell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if(!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
}
Run Code Online (Sandbox Code Playgroud)
在UITableViewSO上搜索了几个问题之后,我想在Swift中写这样的话:
tableView.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: "Cell")
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
Run Code Online (Sandbox Code Playgroud)
但这并没有让我说我想要一种subtitle风格.所以我尝试了这个:
var cell :UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "Cell")
Run Code Online (Sandbox Code Playgroud)
这给了我一个subtitle细胞,但它不让我dequeueReusableCellWithIdentifier.
我已经研究了一些并看了这个视频教程,但他创建了一个单独subclass的UITableViewCell,我认为是不必要的,因为我之前在Obj-C中完成了相同的效果.
有任何想法吗?谢谢.
我已经检查了stackOverFlow的解决方案,但它似乎仍然没有为我的代码工作..我的detailTextLabel仍然没有显示:(
想知道我做错了什么,这是我的代码
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = UITableViewCell()
// display our ranked data
cell.textLabel?.text = "\(championListInGame[indexPath.row])"
if let url = NSURL(string: "http://ddragon.leagueoflegends.com/cdn/img/champion/loading/\(championListInGame[indexPath.row])_0.jpg"){
if let urlData = NSData(contentsOfURL: url){
cell.imageView?.contentMode = UIViewContentMode.ScaleAspectFit
cell.imageView?.image = UIImage(data: urlData)
}
}
if victory[indexPath.row] == true {
cell.backgroundColor = UIColor.blueColor()
cell.detailTextLabel?.text = " "+"victory!"
} else {
cell.backgroundColor = UIColor.redColor()
cell.detailTextLabel?.text = " "+"defeat!"
}
return cell
}
Run Code Online (Sandbox Code Playgroud)
然而,我的胜利和失败都出现在我的桌子中,颜色,图像和文字都像我想要的那样出现.
在旁注,有人可以教我如何选择婴儿蓝或婴儿红等颜色?默认的红色和蓝色对我的应用来说太强了:(
谢谢你的帮助!