xcode uitableview 单元格图像对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5324392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:31:30  来源:igfitidea点击:

xcode uitableview cell image align

xcodeuitableviewcellalignment

提问by nadav

I'm sure this is very simple but i can't find any documentation about it..

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

in my uitableview i have the text labels aligned to the right (my app is in heberw which is rtl) i'm trying to add an image to the cell to the RIGHT of the text label, no luck, so far the image is aligned to the left and i can't find a way to align it to the right side of the cell.

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

here's my code:

这是我的代码:

- (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;

}

回答by HoratioCain

Instead of adding the image as a subview of your UITableViewCell, add a container with right aligned subviews.

不要将图像添加为 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];

This has the added benefit of automatically readjusting when the orientation changes.

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

回答by Vijay-Apple-Dev.blogspot.com

try this one

试试这个

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

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