1

我正在关注一个教程,该教程在表格视图中使用 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

4 回答 4

2
static NSString *cellIdentifier = @"CELLIDENTIFIER";

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

希望对你有用。!!!

于 2015-07-08T10:36:16.447 回答
0

它应该工作。请尝试打击代码。Storyboard 上没有问题。

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

在此处输入图像描述

于 2015-06-08T12:26:07.973 回答
0

将调用更改dequeueReusableCellWithIdentifier:为:

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

这将始终返回一个单元格,因此您永远不必检查 nil。

// if (cell == nil) {
//     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
// }
于 2015-06-08T12:32:39.857 回答
0

我认为上述任何答案都没有解决您的问题。你说得对,这一切都与你的故事板有关。

为了解决这个问题,进入你的 Main.storyboard,点击表格视图并按照下图将视图重置为建议的约束。重置为建议的约束

于 2015-12-26T15:32:49.890 回答