2

在我的表格单元格中添加了标志。使用情节提要将 UIImageView 放在原本为空的原型单元格中并很好地调整其大小(47x34)。然后在这里添加代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell1";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]
            initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = [[[content objectAtIndex:indexPath.section] objectForKey:@"rowValues"] objectAtIndex:indexPath.row];
    cellTxt = cell.textLabel.text;

    UIImageView *flagImg = (UIImageView *) [cell viewWithTag:10];
    NSString *imgFilenm = [cellTxt stringByAppendingString:@".jpg"];
    flagImg.image = [UIImage imageNamed: imgFilenm ];
    cell.imageView.frame = CGRectMake(0, 0, 47, 34);   //  not needed, and doesn't fix it

    return cell;
}

图像出现了,但向左挤压到只有大约 8 个像素。为什么?以及如何纠正?谢谢! 小旗 原件: 伯利兹

** 编辑 ** 我将单元格文本切换为我添加的 UILabel。这是代码:

UILabel *nameLbl = (UILabel *)[cell viewWithTag:5];
nameLbl.text = [[[content objectAtIndex:indexPath.section] objectForKey:@"rowValues"] objectAtIndex:indexPath.row];

以及它在故事板中的样子: 原始细胞

但现在旗子甚至没有出现。

4

2 回答 2

0

让它工作使用:

UIImageView *cellImage = (UIImageView *)[cell viewWithTag:10];
[cellImage setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@.jpg", [self.countries objectAtIndex: [indexPath row]]]]];

不知道为什么其他方法不起作用。

于 2012-11-18T00:28:43.567 回答
0

你的旗帜没有被“压扁”。它们实际上隐藏在单元格的标题后面。尝试类似:

UIImageView *flagImg = (UIImageView *) [cell viewWithTag:10];
NSString *imgFilenm = [cellTxt stringByAppendingString:@".jpg"];
flagImg.image = [UIImage imageNamed: imgFilenm ];
[cell bringSubviewToFront:flagImg];
于 2012-11-17T19:47:21.653 回答