xcode UITableViewCell 图像向右对齐,可能吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9445702/
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
UITableViewCell image alignment to the right, possible?
提问by Yossi Tsafar
when adding an image to table cell, as default it goes to left:
将图像添加到表格单元格时,默认情况下它会向左移动:
cell.imageView.image = [UIImage imageNamed:[arrImages objectAtIndex:indexPath.row]];
how can I change it that every image in the UITableViewCell will go automaticlly to the right and the textLabel will be 10px to the left of the image.
我该如何更改 UITableViewCell 中的每个图像都会自动向右移动,而 textLabel 将在图像左侧 10px 处。
Thanks alot!
非常感谢!
回答by Lorenzo B
Another way is to create a custom cell and override layoutSubviews
method.
另一种方法是创建自定义单元格和覆盖layoutSubviews
方法。
@interface CustomCell : UITableViewCell
@end
@implementation CustomCell
- (void)layoutSubviews
{
[super layoutSubviews];
// grab bound for contentView
CGRect contentViewBound = self.contentView.bounds;
// grab the frame for the imageView
CGRect imageViewFrame = self.imageView.frame;
// change x position
imageViewFrame.origin.x = contentViewBound.size.width - imageViewFrame.size.width;
// assign the new frame
self.imageView.frame = imageViewFrame;
}
@end
Rembember that in cellForRowAtIndexPath
you need to create and reuse CustomCell
and not UITableViewCell
.
请记住,cellForRowAtIndexPath
您需要创建和重用CustomCell
而不是UITableViewCell
.
Hope it helps.
希望能帮助到你。
Edit
编辑
#import "CustomCell.h"
// other code here...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CustomCell";
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
return cell;
}
回答by Narasimha Nallamsetty
Find the solution here code.
在此处找到解决方案代码。
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"foo.png"]];
cell.accessoryView = imageView;
For your reference.
供你参考。
回答by abi
Found a better answer from @TomSwift here https://stackoverflow.com/a/31616694/1884707
在这里从@TomSwift 找到了更好的答案https://stackoverflow.com/a/31616694/1884707
cell.contentView.transform = CGAffineTransformMakeScale(-1,1);
cell.imageView.transform = CGAffineTransformMakeScale(-1,1);
cell.textLabel.transform = CGAffineTransformMakeScale(-1,1);
cell.textLabel.textAlignment = NSTextAlignmentRight;
By applying a transform on the contentView you can place the imageView on the right.
通过在 contentView 上应用转换,您可以将 imageView 放在右侧。
回答by Mohsen mokhtari
in swift3 and swift4, we can use this:
在 swift3 和 swift4 中,我们可以使用这个:
cell.accessoryView = UIImageView(image:UIImage(named:"imageNmae")!)
cell.accessoryView = UIImageView(image:UIImage(named:"imageNmae")!)
回答by meronix
try this:
尝试这个:
cell.imageView.frame = CGRectMake(cell.frame.size.width - cell.imageView.frame.size.width, cell.imageView.frame.origin.y, cell.imageView.frame.size.width, cell.imageView.frame.size.height);
[cell.yourTexLabel sizeToFit];
cell.yourTexLabel.frame = CGRectMake(cell.imageView.origin.x - cell.yourTexLabel.frame.size.width - 10, cell.yourTexLabel.frame.origin.y, cell.yourTexLabel.frame.size.width, cell.yourTexLabel.frame.size.height);
回答by paul.lander
One solution is to use a custom UITableViewCell
. The steps are:
一种解决方案是使用自定义UITableViewCell
. 步骤是:
- Create a new objective-C class that is a subclass of
UITableViewCell
, for exampleLabeledImageTableViewCell
. Declare ivarsand propertiesfor aUILabel
and aUIImageView
. - In Interface Builder, set the contentof the
UITableView
to Dynamic Prototypes. Drag aUIImageView
and aUILabel
to a table view cell and position them. Set the cell's class toLabeledImageTableViewCell
. Connect the outlets of the cell to theUILabel
&UIImageView
objects ofLabeledImageTableViewCell
. In the delegate for
UITableView
(usually aUITableViewController
, sometimes aUIViewController
) implement the datasourcemethods, for example://#pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return (NSInteger)[rowData count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"tvcLabeledImage"; LabeledImageTableViewCell *cell = (LabeledImageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[LNCCorrelationInfoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } cell.myImage = [UIImage imageNamed:@"imageForThisRow.png"]; cell.myLabel = "imageForThisRow"; return cell; }
- 创建一个新的 Objective-C 类,它是 的子类
UITableViewCell
,例如LabeledImageTableViewCell
。声明a和 a 的ivars和属性。UILabel
UIImageView
- 在Interface Builder中,设定的内容的
UITableView
,以动态原型。将 aUIImageView
和 a拖到UILabel
表视图单元格并放置它们。将单元格的类设置为LabeledImageTableViewCell
. 将单元格的出口连接到 的UILabel
&UIImageView
对象LabeledImageTableViewCell
。 在 for
UITableView
(通常是 aUITableViewController
,有时是 aUIViewController
)的委托中实现数据源方法,例如://#pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return (NSInteger)[rowData count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"tvcLabeledImage"; LabeledImageTableViewCell *cell = (LabeledImageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[LNCCorrelationInfoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } cell.myImage = [UIImage imageNamed:@"imageForThisRow.png"]; cell.myLabel = "imageForThisRow"; return cell; }
Also, check out the Apple videos from WWDC 2011, UITableView Changes, Tips & Tricksand Introducing Interface Builder Storyboarding(Login required: https://developer.apple.com/videos/wwdc/2011/.)
此外,请查看 WWDC 2011 的 Apple 视频、UITableView Changes、Tips & Tricks和Introducing Interface Builder Storyboarding(需要登录:https: //developer.apple.com/videos/wwdc/2011/。)