ios/xcode: как добавить вспомогательный вид в пользовательскую ячейку

Я следую руководству, в котором используется пользовательская ячейка, созданная в Storyboard, в виде таблицы, а затем добавляю вспомогательное представление. Таблица загружается хорошо. Однако, когда я пытаюсь добавить дополнительный вид (изображение), ничего не появляется. Могут ли быть какие-то настройки в раскадровке, которые не так? Проблема в коде? Или почему не отображается вид аксессуара

Вот метод, в котором учебник говорит, чтобы добавить вспомогательное представление:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    IDContactImport *contactImport = self.contacts[indexPath.row];
    IDTableViewCell *idTableCell =(IDTableViewCell *)cell;
    idTableCell.contactImport=contactImport;
    if (contactImport.imageData==nil)
    {
        idTableCell.iconView.image = [UIImage imageNamed:@"headshot.jpg"];
    }
    else{
       idTableCell.iconView.image = [UIImage imageWithData:contactImport.imageData];
    }
//HERE IS ACCESSORY VIEW
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checkbox.jpg"]];
    idTableCell.accessoryView = imageView;
//   [self updateAccessoryForTableCell:cell atIndexPath:indexPath];
          // Configure the cell...
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    return cell;
}

Спасибо за любые предложения

4 ответа

static NSString *cellIdentifier = @"CELLIDENTIFIER";

UITableViewCell *customCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
UIImageView *sampleImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sampleImage.png"]];
[customCell setAccessoryView:sampleImage];

Надеюсь, что это полезно для вас.!!!

Он должен работать. Пожалуйста, попробуйте код удара. На раскадровке нет проблем.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"customCell";
    CustomCellView *cell = [ctableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    long row = [indexPath row];
    cell.name.text = carname[row];
    cell.model.text = carModel[row];
    cell.carImg.image = [UIImage imageNamed:carImage[row]];

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SelectedStar_green.png"]];
    cell.accessoryView = imageView;

    // Configure the cell...

    return cell;
}

Изменить dequeueReusableCellWithIdentifier: позвонить:

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

Это всегда возвращает ячейку, поэтому вам никогда не придется проверять на ноль.

// if (cell == nil) {
//     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
// }

Я не думаю, что какой-либо из вышеперечисленных ответов касался вашей проблемы. Вы правы, упомянув, что все это связано с вашей раскадровкой.

Чтобы это исправить, перейдите на свою Main.storyboard, нажмите на табличное представление и сбросьте представление к предлагаемым ограничениям, следующим за рисунком ниже. Сброс к предложенным ограничениям

Другие вопросы по тегам