如何在 iOS 5 故事板中初始化自定义原型样式表单元格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9334156/
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
How to initialize a custom prototype style table cell in iOS 5 storyboards?
提问by Tom Kincaid
I am converting over to iOS 5 and storyboards. When I have a table view with default cell style, everything works fine.
我正在转换为 iOS 5 和故事板。当我有一个默认单元格样式的表格视图时,一切正常。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifierFromStoryboard"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyIdentifierFromStoryboard"];
}
return cell;
}
I have seen examples where the "if (cell == nil)" block is removed. However if I take it out, my app crashes with the message: "UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:". This is not a problem because it works as shown above.
我见过删除“if (cell == nil)”块的例子。但是,如果我将其取出,我的应用程序会崩溃并显示以下消息:“UITableView 数据源必须从 tableView:cellForRowAtIndexPath: 返回一个单元格”。这不是问题,因为它的工作原理如上所示。
My problem is that I want to use a custom style for the cell and thus cannot use initWithStyle. How do I initialize a custom cell that I have designed on the storyboard?
我的问题是我想为单元格使用自定义样式,因此不能使用 initWithStyle。如何初始化我在故事板上设计的自定义单元格?
The old pre-5 app had a nib and class that used something like this, but now I'm using the storyboard.
旧的 pre-5 应用程序有一个使用类似这样的东西的笔尖和类,但现在我正在使用故事板。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyCustomTableCell *cell = (MyCustomTableCell *) [tableView dequeueReusableCellWithIdentifier:@"MyCustomIdentifier"];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyCustomTableCell" owner:self options:nil];
cell = (MyCustomTableCell *) [nib objectAtIndex:0];
}
return cell;
}
回答by Alex
This way
这边走
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
return cell;
}
回答by OSXMonk
This worked perfect for me (Xcode 4.5.2 and iOS 6.0):
这对我来说很完美(Xcode 4.5.2 和 iOS 6.0):
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if( cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
UILabel *title = (UILabel*) [cell viewWithTag:1000];
UILabel *summary = (UILabel*) [cell viewWithTag:1001];
[title setText:[ tableMainTitle objectAtIndex:indexPath.row]];
[summary setText:[ tableSubTitle objectAtIndex:indexPath.row]];
return cell;
}
Important: Do not forget setting delegate and datasource.
重要提示:不要忘记设置委托和数据源。
回答by jemeshsu
If you load your view controller that contains the tableview using:
如果您使用以下方法加载包含 tableview 的视图控制器:
MyViewController *myViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MyViewController"];
Then inside cellForRowAtIndexPath
you just need one line:
然后在里面cellForRowAtIndexPath
你只需要一行:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifierFromStoryboard"];
dequeueReusableCellWithIdentifier
will instantiate one cell if there is none exists.
dequeueReusableCellWithIdentifier
如果不存在,将实例化一个单元格。
回答by fftoolbar
I think this one works very well. I was trying to get my head around it for sometime and thanks it worked.. well i was doing this, when trying to alloc a cell i was allocating using the general cell class instead of the one i had created
我认为这个效果很好。一段时间以来,我一直在努力解决这个问题,感谢它奏效了……好吧,我正在这样做,当我尝试分配一个单元格时,我正在使用通用单元格类而不是我创建的单元格类进行分配
so i was doing this, so thats reason why it did not work
所以我正在这样做,所以这就是它不起作用的原因
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
so instead of UITableViewCell alloc the cell with your class, well with the above example "CustomCell"
因此,而不是 UITableViewCell 为您的类分配单元格,以及上面的示例“CustomCell”
Thanks again
再次感谢
回答by Anand
static NSString *identifier=@"SearchUser";
SearchUserCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell==nil)
{
cell=[[SearchUserCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
return cell;