简体   繁体   中英

Missing accessory in custom UITableViewCell

Before I created custom cell for UITableView I used this to render rows:

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.
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..

Hope it fix the issue..

If you're using storyboard, then you have to register your custom nib in 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

- (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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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