我如何在相同的xib中使用自定义UITableViewCell和UITableView [英] How can i use custom UITableViewCell and UITableView in same xib
问题描述
我想在同一个xib中使用带有UITableView的自定义UITableviewCell,而不创建UITableViewCell类?
I want to use a custom UITableviewCell with UITableView in same xib without creating a UITableViewCell class?
正如您所见,我为UITableViewCell设置了标识符并使用了它像这样:
As you can see bellow i set the identifier for UITableViewCell and used it like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CustomIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
}
但不工作?
推荐答案
将UITableViewCell * customCell属性添加到视图控制器(例如,您的文件ShowUsers.h)......
Add a UITableViewCell *customCell property to your view controller (for example, your file ShowUsers.h)...
@property (nonatomic, strong) IBOutlet UITableViewCell *customCell;
并将其连接到xib中的自定义单元格。然后在cellForRow中使用以下代码(例如,您的文件ShowUsers.m):
and connect it to the custom cell in your xib. Then use the following code in cellForRow (for example, your file ShowUsers.m) :
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"NibName" owner:self options:nil];
cell = self.customCell;
self.customCell = nil;
}
这篇关于我如何在相同的xib中使用自定义UITableViewCell和UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!