【问题标题】:xcode uitableview cell image alignxcode uitableview 单元格图像对齐
【发布时间】:2011-07-16 12:20:07
【问题描述】:

我确信这很简单,但我找不到任何关于它的文档..

在我的 uitableview 中,我的文本标签向右对齐(我的应用程序在 heberw 中,即 rtl) 我正在尝试将图像添加到文本标签右侧的单元格中,没有运气,到目前为止,图像与左侧对齐,我找不到将其与单元格右侧对齐的方法。

这是我的代码:

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }


        cell.imageView.image = [UIImage imageNamed:@"tableicon.png"];

    NSString *cellValue = [[stories objectAtIndex:indexPath.row] objectForKey:@"ArticleTitle"];
    cell.textLabel.text = cellValue;
    cell.textLabel.textAlignment = UITextAlignmentRight;
    cell.textLabel.font = [UIFont systemFontOfSize:17];
        cell.textLabel.numberOfLines = 2;

    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;

}

【问题讨论】:

    标签: xcode uitableview cell alignment


    【解决方案1】:

    不要将图像添加为 UITableViewCell 的子视图,而是添加一个具有右对齐子视图的容器。

    UIImageView *pic = myImageView;
    UIView *picContainer = [[UIView alloc] initWithFrame:cell.frame];
    picContainer.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    picContainer.contentMode = UIViewContentModeRight;
    pic.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
    pic.center = CGPointMake(picContainer.frame.size.width-70, picContainer.frame.size.height/2);
    [picContainer addSubview:pic];
    [cell.contentView addSubview:picContainer];
    

    这具有在方向改变时自动重新调整的额外好处。

    【讨论】:

      【解决方案2】:

      试试这个

      cell.accessoryView=[UIImage imageNamed:@"tableicon.png"];

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-10-13
        • 1970-01-01
        • 2015-11-13
        • 1970-01-01
        • 2019-07-25
        • 2019-04-24
        • 1970-01-01
        相关资源
        最近更新 更多