Ios5 如何在UIViewController中实现的UITableView中控制自定义UITableViewCell?

Ios5 如何在UIViewController中实现的UITableView中控制自定义UITableViewCell?,ios5,uitableview,custom-controls,Ios5,Uitableview,Custom Controls,在我的应用程序中,我希望有一个UIViewController,它将在其中保存一个UITableView。在此UITableView中,我希望有一个自定义的UITableViewCell(即,我希望在此单元格中定义我自己的元素-图像、标签和按钮-如下图所示)。而且。。。我想在故事板中创建它们 现在,在故事板中设置元素很容易 我了解如何连接UITableView并在UIViewController中设置它(包括.h文件中的委托并使用基本表委托方法) 我不清楚的是,如何连接和控制定制的UITabl

在我的应用程序中,我希望有一个UIViewController,它将在其中保存一个UITableView。在此UITableView中,我希望有一个自定义的UITableViewCell(即,我希望在此单元格中定义我自己的元素-图像、标签和按钮-如下图所示)。而且。。。我想在故事板中创建它们

  • 现在,在故事板中设置元素很容易
  • 我了解如何连接UITableView并在UIViewController中设置它(包括.h文件中的委托并使用基本表委托方法)
  • 我不清楚的是,如何连接和控制定制的UITableViewCell及其插座。我可以在UIViewController.h和.m文件中创建出口和操作吗?我是否需要创建一个单独的UITableViewCell.h/.m文件,并在cellForRowAtIndexPath方法中调用它们
有人能建议什么是满足我需求的最佳方法吗

更新: 下面是我在使用分离的MyCell.h/m文件选项时在cellforrowatinexpath中使用的代码。此代码编写在实现UITableView的ViewController.m文件中

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    static NSString *CellIdentifier = @"ContentCell";

    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

//MyCell is the Objective-C Class I created to manage the table cells attributes.

//@"ContentCell" is what I had entered in the Storyboard>>UITableViewCell as the Cell Identifier.

    if (cell == nil) {
        cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//Here is the place I'm not clear about - Am I supposed to init the cell in a different way? If so, how?    
    }

    cell.contentNameLabel.text = [self.dataArray objectAtIndex: [indexPath row]];

// this is the test I had done to check if the new "MyCell" object is actually getting what I would expect it to get. well... the text gets updated in the relevant label, so i guess it gets it 

    return cell;

}

在运行应用程序时,使用调试器断点,我可以看到代码总是跳过“if(cell==nil)”并且从不进入新的MyCell对象假定分配和启动的代码。知道我做错了什么吗?

正确,创建单独的UITableViewCell.h/.m文件以匹配自定义UITableViewCell类,并在cellForRowAtIndexPath方法中调用它们

在情节提要中,将自定义UITableViewCell的类设置为自定义类(例如CustomTableCell)

自定义UITableViewCell类将包含IBOutlets,您可以将其连接到情节提要中,以下是一个示例:

CustomTableCell.h:

#import "CustomStuff.h" // A custom data class, for this example

@interface CustomTableCell : UITableViewCell

@property (nonatomic, weak) IBOutlet UILabel *titleLabel;

- (void)configureForCustomStuff:(CustomStuff *)stuff;

@end
CustomTableCell.m:

#import "CustomTableCell.h"

@implementation CustomTableCell

@synthesize titleLabel;

#pragma mark - Configure the table view cell

- (void)configureForCustomStuff:(CustomStuff *)stuff
{
    // Set your outlets here, e.g.
    self.titleLabel.text = stuff.title;
}

@end
然后,在cellForRowAtIndexPath方法中,配置单元格:

CustomTableCell *cell = (CustomTableCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomCellID"];

// Your custom data here
CustomStuff *customStuff = <YOUR CUSTOM STUFF>

[cell configureForCustomStuff:customStuff];
CustomTableCell*单元格=(CustomTableCell*)[tableView dequeueReusableCellWithIdentifier:@“CustomCellID]”;
//您的自定义数据在此显示
CustomStuff*CustomStuff=
[customStuff的单元格配置:customStuff];

谢谢Marco的详细回答。尽管如此,当我实施它的时候,有些东西并没有正常工作;也许你会对此有所了解。我能够创建表格并用我的数据填充单元格标签(从中我可以看出单元格是正确创建的);但是didSelectRowAtIndexPath不起作用。当我测试它并点击某一行时,应用程序崩溃;我在DidSelectRowatineXpath中有一个断点,而应用程序从未到达该断点。您是否在故事板中设置了表视图单元格标识符,以及它在CellForRowatineXpath中是否匹配:?嗨,Marco,我在我的原始问题中添加了我实现CellForRowatineXpath的方式。我想我做错了什么,但我不能完全肯定。你能看一下吗?