【问题标题】:UITableViewCell image viewUITableViewCell 图像视图
【发布时间】:2016-08-05 16:39:43
【问题描述】:

我用自己的自定义UITableViewCell 创建了UITableView,我有一个UITableViewCell 的.xib 文件,我的应用程序语言是从右到左我想在单元格右侧显示UIImageView 并自定义我的细胞用于此目的。但是在UITableView更改表格视图模式并选择单元格时,我的图像视图显示在单元格左侧并更改了此位置,问题出在哪里?

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  PersonnelCell *cell = (PersonnelCell *)[self.tableView dequeueReusableCellWithIdentifier:@"PersonnelCell"];
  NSMutableArray *cells = [[NSMutableArray alloc] init];

  if (cell == nil) {
    cell = [[PersonnelCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"PersonnelCell"];
  }
  cell.cellImage.image = [UIImage imageNamed:@"logo.png"];

  NSArray *personnels = [[PersonelList sharedList] allPersonel];

  if (tableView == self.searchDisplayController.searchResultsTableView) {

    NSArray *a = [[NSArray alloc] init];
    a = [self.resultData firstObject];

    NSString *s = [[a objectAtIndex:0] stringByAppendingString:@" "];
    s = [s stringByAppendingString: [a objectAtIndex:1]];

    cell.nameLabel.text = s;
    if ([[a objectAtIndex:2] isKindOfClass:[UIImage class]]) {

        cell.cellImage.image=[a objectAtIndex:2];
    }
  return cell;
}

【问题讨论】:

  • 显示您的代码和模拟器快照。
  • 可能是因为tableview重用了单元格,所以第二次分配单元格时使用的是默认单元格。

标签: ios objective-c uitableview right-to-left


【解决方案1】:

我不能发表评论,它要求我有 50 次以上的代表。无论如何,要解决您的问题,您必须在视图中使用约束。在 Xibs 中应用约束比以编程方式执行要容易得多。

【讨论】:

  • 我为单元格中的所有视图添加约束
【解决方案2】:

每个单元格都有默认的图像视图,例如cell.imageView

可能您将图像设置为单元格的默认图像视图,这就是它位于左侧的原因

尝试更改图像视图的 IBOutlet

如果不起作用,请尝试设置约束

顶部、前导和固定宽度、高度

【讨论】:

  • 我正在尝试。删除 .h 文件中图像视图的 IBOutlet 并使用另一个名称为损坏视图创建新的出口,我也有这个问题。
【解决方案3】:

示例代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FAQCellIdentifier];

        if (!cell) {
            UINib *nib = [UINib nibWithNibName:@"FAQTableViewCell" bundle:nil];
            [tableView registerNib:nib forCellReuseIdentifier:FAQCellIdentifier];
            cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:FAQCellIdentifier];
        }

        FAQ *oFAQ = [self.mainList objectAtIndex:indexPath.row];

        cell.lblTitle.text = oFAQ.question;
        cell.yourImgView.image = [UIImage imageNamed: oFAQ.imgName];
        return cell;
    }

另外请检查您是否将 imageView 的约束设置为顶部、尾部、固定:宽度、高度

【讨论】:

  • 我的图像视图高度和宽度等于30,当改变应用程序图像视图中的图像视图状态为40 * 40时,这意味着我的图像视图替换为cell.image视图
  • @Qazal 你能显示你的代码和模拟器截图吗?
  • 我的声誉等于 4 我无法添加图像。
【解决方案4】:
#import "CustomCell.h"

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"ID_CustomCell";

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil){

    cell = (CustomCell *)[[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil] objectAtIndex:0];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

return cell;
}

Download custom cell from

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多