我试图为UIAccessoryView使用一个自定义映像,但是我似乎无法让它工作。当表视图启动时,它不会使用任何图像进行更新。我的代码如下:
- (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中,但是似乎没有什么能奏效。除了自己创造一个全新的配饰观之外,有什么正确的方法来解决这个问题呢?
发布于 2013-09-06 20:02:07
由于最初使用UIImageView图像设置nil,图像视图的帧宽度和高度为零。您需要在指定实际图像后重置帧。
imageView.image = [UIImage imageNamed:@"DisclosureViewX"];
imageView.frame = CGRectMake(0, 0, imageView.image.size.width, imageView.image.size.height);编辑:
实际上,在设置映像之后调用[imageView sizeToFit]甚至比设置frame更容易。
发布于 2013-09-06 20:01:55
试着做
tableViewCell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"DisclosureViewX"]];发布于 2013-09-06 20:27:43
我认为您的问题是图像视图创建的大小为零。使用initWithFrame:或initWithImage:,其中图像不是零。使用initWithImage:创建之后,图像视图的边界将设置为图像大小。单元格将自动对附件视图进行居中。
https://stackoverflow.com/questions/18665584
复制相似问题