UITmageView在UITableViewCell中

Dim*_*mme 0 cocoa-touch objective-c uitableview uiimageview

我正在尝试在UITableViewCell中显示UIImageView.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath我有内部方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

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


    CGSize cellSize = cell.frame.size;
    CGFloat cellWidth = cellSize.width;
    CGFloat cellHeight = cellSize.height;

    CGRect imageFrame = CGRectMake(0, 0, 70, cellHeight);
    UIImageView * image = [[UIImageView alloc] initWithFrame:imageFrame];
    [image setImage:[UIImage imageWithContentsOfFile:@"cyan.jpg"]];

    CGRect nameFrame = CGRectMake(80, 0, cellWidth-80, cellHeight/2);
    UILabel * nameLabel = [[UILabel alloc] initWithFrame:nameFrame];
    nameLabel.text = @"Name: John Doe";

    CGRect jobFrame = CGRectMake(80, 20, cellWidth-80, cellHeight/2);
    UILabel * jobLabel = [[UILabel alloc] initWithFrame:jobFrame];
    jobLabel.text = @"Job: IT-Consultant";

    [cell addSubview:image];
    [cell addSubview:nameLabel];
    [cell addSubview:jobLabel];

    return cell;
}
Run Code Online (Sandbox Code Playgroud)

标签显示完美但我看不到图像.

任何帮助,将不胜感激.

jrt*_*ton 6

imageWithContentsOfFile:期望将路径发送给它.imageNamed:改为使用,或者首先使用NSBundle's pathForResource ...方法来获取路径.

此外,如果每个单元格的图像相同,则应在cell = nil块内添加图像视图,否则您将反复添加视图.