简体   繁体   English

如何在iOS 5故事板中初始化自定义原型样式表单元格?

[英]How to initialize a custom prototype style table cell in iOS 5 storyboards?

I am converting over to iOS 5 and storyboards. 我正在转换到iOS 5和故事板。 When I have a table view with default cell style, everything works fine. 当我有一个默认单元格样式的表视图时,一切正常。

- (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. 我已经看到了删除“if(cell == nil)”块的示例。 However if I take it out, my app crashes with the message: "UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:". 但是,如果我把它拿出来,我的应用程序会崩溃并显示以下消息:“UITableView dataSource必须从tableView返回一个单元格:cellForRowAtIndexPath:”。 This is not a problem because it works as shown above. 这不是问题,因为它的工作原理如上所示。

My problem is that I want to use a custom style for the cell and thus cannot use initWithStyle. 我的问题是我想为单元格使用自定义样式,因此不能使用initWithStyle。 How do I initialize a custom cell that I have designed on the storyboard? 如何初始化我在故事板上设计的自定义单元格?

The old pre-5 app had a nib and class that used something like this, but now I'm using the storyboard. 旧的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;
}

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;
}

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. 重要提示:不要忘记设置委托和数据源。

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将实例化一个单元格。

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 再次感谢

static NSString *identifier=@"SearchUser";
SearchUserCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell==nil)
{
    cell=[[SearchUserCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
return cell;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM