xcode 自定义 UITableViewCellStyle
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8194442/
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
custom UITableViewCellStyle
提问by werbary
I want to make custom uitableviewcellstyle to make comments in my app. I want uitableviewcell with comment text, number of likes, author's name, date etc... Have you any ideas? I've created method, but I don't know how realize it. My code:
我想自定义 uitableviewcellstyle 以在我的应用程序中发表评论。我想要带有评论文本、喜欢数量、作者姓名、日期等的 uitableviewcell ......你有什么想法吗?我已经创建了方法,但我不知道如何实现它。我的代码:
- (UITableViewCell *)getCommentTableCellWithTableView:(UITableView *)tableView commentText:(NSString *)commentText numberOfRows:(NSInteger)numberOfRows numberOfLikes:(NSString *)numberOfLikes date:(NSString *)date writer:(NSString *) writerName {
static NSString *CellIdentifier = @"TitleCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
return cell;
}
回答by Kjuly
I'm sorry I can't find a tutorial with clear steps, but you can search some related posts or questions on this site.
Hope the simple code below could help you.
很抱歉,我找不到步骤清晰的教程,但您可以在本网站上搜索一些相关帖子或问题。
希望下面的简单代码可以帮助您。
Here is a doc may helps too, take your time to have a look;)
http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/TableView_iPhone/TableViewCells/TableViewCells.html
这里的文档也可能有帮助,请花点时间看看;)
http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/TableView_iPhone/TableViewCells/TableViewCells.html
New class inherit from UITableViewCell, CustomCell.h:
(Tips:File
->New File
->Objective-C class
->set class name & choose the subclass UITableViewCell
)
新类继承自 UITableViewCell, CustomCell.h:
(Tips: File
-> New File
-> Objective-C class
->set class name & select the subclass UITableViewCell
)
@interface MapsListViewCell : UITableViewCell
{
// Add iVars that you want.
}
// Some custom methods
CustomCell.m:
CustomCell.m:
// ...
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
// ...
// Some custom methods
- (void)setAuthorName:(NSString *)name
{
// ...
}
TableViewController.m:
TableViewController.m:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CategoriesListViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
CategoriesListViewCell * customCell = (CategoriesListViewCell *)cell;
// Set your data:
[customCell setAuthorName:@"God"];
// ...etc.
return cell;
}
回答by Daniel Galasko
For those of us using the newer (iOS 6 and up) UITableView
API for dequeueing cells namelydequeueReusableCellWithIdentifier:forIndexPath
this is actually guaranteed to return an instantiated cell so we can't perform the nil check and manually call initWithStyle
. Therefore the best solution is to subclass UITableViewCell
and manually enforce the style in initialisation.
对于我们这些使用较新(iOS 6 及更高版本)UITableView
API 出列单元格的人来说,dequeueReusableCellWithIdentifier:forIndexPath
这实际上保证返回一个实例化的单元格,因此我们无法执行 nil 检查并手动调用initWithStyle
. 因此,最好的解决方案是UITableViewCell
在初始化时子类化并手动强制执行样式。
So as an example if we wanted a cell with the UITableViewCellStyleSubtitle
style we would create a custom subclass:
例如,如果我们想要一个具有UITableViewCellStyleSubtitle
样式的单元格,我们将创建一个自定义子类:
@interface ABCSubtitledTableViewCell : UITableViewCell
@end
@implementation ABCSubtitledTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
return [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
}
@end
And then in our viewDidLoad
we would register the appropriate class
然后在我们的viewDidLoad
我们将注册适当的类
[tableView registerClass:[ABCSubtitledTableViewCell class] forCellReuseIdentifier:NSStringFromClass([ABCSubtitledTableViewCell class])];
Making our dequeue method:
制作我们的出队方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ABCSubtitledTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ABCSubtitledTableViewCell class]) forIndexPath:indexPath];
cell.textLabel.numberOfLines = 0;
cell.textLabel.text = @"Hello";
cell.detailTextLabel.text = @"World";
return cell;
}