我试图将tableview中的单元格从泛型更改为自定义并创建一个新的tableview单元类。但是,我无法让单元格识别新的自定义单元格。相反,它仍然显示通用单元格,虽然我已将故事板中的设置更改为标识检查器中的自定义,并且还将tableview单元格的类更改为新的自定义单元格。我还将故事板中单元格中的元素连接到新的自定义类。
我的理解是tableview VC知道从tableView cellForRowAtIndexPath
方法使用哪个类,我也改变了下面的代码。该项目编译但继续显示旧的通用单元格。任何人都可以看到错误或建议还有什么可做的吗?
#import "customCell.h"
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
customCell *cell = (customCell *)[self.tableView dequeueReusableCellWithIdentifier:@"Cell"];
Items *item = [self.fetchedResultsController objectAtIndexPath:indexPath];
return cell;
}
在此先感谢您的任何建议。
编辑:
通过拖动uielements在tableview顶部的故事板中创建自定义单元格。也许这是个问题:
单元格样式设置为自定义:
您需要创建新的UITableViewCell并启用“创建XIB文件...”。在XIB中创建自定义单元格,并将其自定义类作为您刚刚创建的类。
然后在tableView cellForRowAtIndexPath
注册Nib :(它很快但我打赌你可以找出目标c并行......)
tableView.registerNib(UINib(nibName: "YourUITableViewCellClass", bundle: nil), forCellReuseIdentifier: "YourCustomIdentifierFromTheCustomClass")
然后访问您的单元格:
var cell = tableView.dequeueReusableCellWithIdentifier("YourCustomIdentifierFromTheCustomClass")
就是这样。
您可以通过拖动单元格视图在主故事板中创建自定义单元格。
为该原型创建自定义类。
提到标识符检查器中的类名。
将该类导入视图控制器.h文件。
并将您的自定义从主故事板连接到自定义class.h文件。
有用!。
创建自定义单元格时,您必须执行以下操作:
- (UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath
{
static NSString *MyIdentifier = @"MyCustomCell";
CustomCell*cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier] autorelease];
}
// Here we use the provided setImageWithURL: method to load the web image
// Ensure you use a placeholder image otherwise cells will be initialized with no image
[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://example.com/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder"]];
cell.myCustomLabel.text = @"My Text";
return cell; }