繁体   English   中英

自定义UITableViewCell中缺少附件

[英]Missing accessory in custom UITableViewCell

在为UITableView创建自定义单元格之前,我使用了它来渲染行:

static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

然后,我创建了CustomCell两个UILabels它。
并将上面的代码替换为:

static NSString *CellIdentifier = @"customCell";

    OrderCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil){
        [tableView registerNib:[UINib nibWithNibName:@"MyCustomCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

但是现在附件不可见。 它仅在第一行显示,与以前不同。
创建自定义单元格时,我还需要做些什么来显示附件?

如下修改您的代码。 在条件之外设置附件类型。

static NSString *CellIdentifier = @"customCell";

OrderCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if(cell == nil){
    [tableView registerNib:[UINib nibWithNibName:@"MyCustomCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


}
 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

您已经在条件if(cell == nil)中设置了accessoryType,该条件是第一次调用。

希望它能解决问题。

如果使用情节提要,则必须在viewDidLoad注册自定义笔尖

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"YourClass" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"YourCellIdentifier"];
}

如果您不使用情节提要,则必须在cellForRowAtIndexPath进行注册

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"YourCellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if(!cell)
    {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"YourCustomClass" owner:self options:nil] lastObject];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    ////

    return cell;
}

您必须在viewDidLoad中注册笔尖,而不是在cellForRowAtIndexPath中注册

- (void)viewDidLoad
{
   [super viewDidLoad];
   static NSString *CellIdentifier = @"customCell";
   [tableView registerNib:[UINib nibWithNibName:@"MyCustomCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];
}

暂无
暂无

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

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