简体   繁体   English

自定义UITableViewCell中缺少附件

[英]Missing accessory in custom UITableViewCell

Before I created custom cell for UITableView I used this to render rows: 在为UITableView创建自定义单元格之前,我使用了它来渲染行:

static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

Then I have created CustomCell with two UILabels in it. 然后,我创建了CustomCell两个UILabels它。
And replace code from above with: 并将上面的代码替换为:

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

But now accessory is not visible. 但是现在附件不可见。 It shows only at first row and it doesn't look like before. 它仅在第一行显示,与以前不同。
Is there anything else that I need to do to show accessory when creating custom cell? 创建自定义单元格时,我还需要做些什么来显示附件?

Modify your code as below. 如下修改您的代码。 Set the accesssory Type outside of condition.. 在条件之外设置附件类型。

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;

You have set the accessoryType inside the condition if(cell==nil) which calls first time.. 您已经在条件if(cell == nil)中设置了accessoryType,该条件是第一次调用。

Hope it fix the issue.. 希望它能解决问题。

If you're using storyboard, then you have to register your custom nib in viewDidLoad 如果使用情节提要,则必须在viewDidLoad注册自定义笔尖

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

If you're not using storyboard, you have to register it in your cellForRowAtIndexPath 如果您不使用情节提要,则必须在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;
}

You have to register your nib in viewDidLoad instead of cellForRowAtIndexPath 您必须在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