ios / xcode:如何将附件视图添加到自定义单元格

时间:2015-06-08 12:09:52

标签: ios uitableview accessoryview

我正在遵循一个教程,该教程使用在tableview中在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 个答案:

答案 0 :(得分: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];

希望它对你有用。!!!

答案 1 :(得分:1)

它应该工作。请尝试吹码。故事板上没有问题。

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

enter image description here

答案 2 :(得分:0)

dequeueReusableCellWithIdentifier:来电更改为:

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

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

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

答案 3 :(得分:0)

我不认为以上任何一个答案都解决了您的问题。你是对的,提到这一切都与你的故事板有关。

要解决此问题,请进入Main.storyboard,单击表格视图,然后将视图重置为建议的约束,如下图所示。 Resetting to suggested constraints