首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIImageView在accessoryView中的UITableViewCell应用

UIImageView在accessoryView中的UITableViewCell应用
EN

Stack Overflow用户
提问于 2013-09-06 19:53:07
回答 3查看 1.1K关注 0票数 1

我试图为UIAccessoryView使用一个自定义映像,但是我似乎无法让它工作。当表视图启动时,它不会使用任何图像进行更新。我的代码如下:

代码语言:javascript
复制
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *tableViewCell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    MenuItem *menuItem = [self.menuItems objectAtIndex:indexPath.row];

    if (!tableViewCell)
    {
        tableViewCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        tableViewCell.textLabel.font = [UIFont fontWithName: CaptionDistractionBodyFontName size: 15];
        imageView.contentMode = UIViewContentModeCenter;
        tableViewCell.accessoryView = [[UIImageView alloc] initWithImage:nil];
    }

    tableViewCell.textLabel.text = menuItem.name;

    UIImageView *imageView = (UIImageView*)tableViewCell.accessoryView;
    imageView.image = nil;

    if (menuItem.type == MenuItemTypeCheckMark)
    {
        if (menuItem.isCheckMarked)
        {
            imageView.image = [UIImage imageNamed:@"DisclosureViewX"];
        }
    }
    else if (menuItem.type == MenuItemTypeSubmenu)
    {
        imageView.image = [UIImage imageNamed:@"DisclosureViewArrow"];
    }

    return tableViewCell;
}

我尝试过很多不同的东西,比如调用setNeedsLayout,在layoutSubviews中将代码移动到自定义的UITableViewCell中,但是似乎没有什么能奏效。除了自己创造一个全新的配饰观之外,有什么正确的方法来解决这个问题呢?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-09-06 20:02:07

由于最初使用UIImageView图像设置nil,图像视图的帧宽度和高度为零。您需要在指定实际图像后重置帧。

代码语言:javascript
复制
imageView.image = [UIImage imageNamed:@"DisclosureViewX"];
imageView.frame = CGRectMake(0, 0, imageView.image.size.width, imageView.image.size.height);

编辑:

实际上,在设置映像之后调用[imageView sizeToFit]甚至比设置frame更容易。

票数 2
EN

Stack Overflow用户

发布于 2013-09-06 20:01:55

试着做

代码语言:javascript
复制
tableViewCell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"DisclosureViewX"]];
票数 1
EN

Stack Overflow用户

发布于 2013-09-06 20:27:43

我认为您的问题是图像视图创建的大小为零。使用initWithFrame:或initWithImage:,其中图像不是零。使用initWithImage:创建之后,图像视图的边界将设置为图像大小。单元格将自动对附件视图进行居中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18665584

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档