xcode iOS Swift - 自定义 UITableViewCell
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24298354/
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
iOS Swift - Custom UITableViewCell
提问by suntzu
I am sure the question itself has been asked and answered properly but in Objective C. I am using swift and was wondering how to customize a UITableViewCell properly. I followed this tutorial here http://www.appcoda.com/customize-table-view-cells-for-uitableview/but I am stuck at properly initializing and using the custom class and XIB file I created. yes, I am a noob. Here is what I have for the standard cell without customization:
我确信问题本身已经被提出并正确回答,但在目标 C 中。我正在使用 swift 并且想知道如何正确自定义 UITableViewCell。我在这里遵循本教程http://www.appcoda.com/customize-table-view-cells-for-uitableview/但我坚持正确初始化和使用我创建的自定义类和 XIB 文件。是的,我是菜鸟。这是我没有定制的标准单元格的内容:
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "myCell")
cell.text = postMgr.posts[indexPath.section].title
cell.detailTextLabel.text = postMgr.posts[indexPath.section].description
return cell
}
If someone can translate the Obj C in the tutorial to swift that would be great. Here it is:
如果有人可以将教程中的 Obj C 翻译成 swift 那就太好了。这里是:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"SimpleTableCell";
SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.nameLabel.text = [tableData objectAtIndex:indexPath.row];
cell.thumbnailImageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
cell.prepTimeLabel.text = [prepTime objectAtIndex:indexPath.row];
return cell;
}
}
Not sure if that's even how it works with iOS7/8. If someone has a better and easier way of customizing the cell, let me know in Swift language.
不确定这是否是 iOS7/8 的工作方式。如果有人有更好更简单的自定义单元格的方法,请用 Swift 语言告诉我。
I appreciate the help already. I am a beginner, please bear with me :)
我已经很感激你的帮助了。我是初学者,请多多包涵:)
KM
知识管理
回答by Anil Varghese
I just ported the tutorial sample app to Swift
. I'am still using the same CustomTableViewCell written in objective-C (used bridging header to avail the class). My cellForRowIndexPath
looks like
我刚刚将教程示例应用程序移植到Swift
. 我仍在使用用 Objective-C 编写的相同 CustomTableViewCell(使用桥接头来利用该类)。我的cellForRowIndexPath
样子
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
{
var cell:SimpleTableCell! = tableView.dequeueReusableCellWithIdentifier(simpleTableIdentifier) as? SimpleTableCell
if (cell == nil)
{
let nib:Array = NSBundle.mainBundle().loadNibNamed("SimpleTableCell", owner: self, options: nil)
cell = nib[0] as? SimpleTableCell
}
cell.nameLabel.text = tableData[indexPath.row]
cell.thumbnailImageView.image = UIImage(named:thumbnails[indexPath.row])
cell.prepTimeLabel.text = prepTime[indexPath.row];
return cell;
}
find the complete source code here: TableViewApp-Swift
在这里找到完整的源代码:TableViewApp-Swift