Beh*_*çar 10 iphone uitableview imageview
我正在尝试将各种大小的图像放在UITableViewCell的imageView中.我得到图像数据asynch'ly,创建图像,设置imageView的内容模式,最后设置imageView的边界.但是代码似乎对我所做的任何更改都不敏感.我希望图像以75x75的区域为中心.我为此目的编写了以下代码
UIImage* image = [UIImage imageWithData:data];
[holder.imageView setContentMode:UIViewContentModeCenter || UIViewContentModeRedraw];
[holder.imageView setImage:image];
[holder.imageView setBounds:CGRectMake(0,0,75,75)];
[holder.imageView setFrame:CGRectMake(0,0,75,75)];
[holder setNeedsLayout];
Run Code Online (Sandbox Code Playgroud)
持有者是UITableViewCell.我得到的结果总是一样的.所有图像都有75px的高度和不同的宽度.有人可以帮我解决这个问题吗?
我已经意识到设置contentMode和bounds属性对该代码没有任何影响.我在最后一行之后添加了一个NSLog,得到如下结果:
NSLog(@"imageview:%@ bounds and contentMode:%@ %@",[holder imageView],[holder.imageView bounds],[holder.imageView contentMode]);
Run Code Online (Sandbox Code Playgroud)
imageview:<UIImageView:0x39ab8a0; frame =(0 0; 75 75); opaque = NO; userInteractionEnabled = NO; layer = <CALayer:0x39a92b0 >> bounds和contentMode:(null)(null)
仍然没有解决方案
Beh*_*çar 42
完成后,我终于找到了解决方案,虽然我花了3个小时=)
解决方案是在自定义UITableViewCell类的 - (void)layoutSubviews方法中更改bound,frame,contentMode等属性."技巧"是在此方法中编写布局代码,否则代码没有任何影响.
下面的代码为我做了工作.它使表格的行垂直对齐.
- (void)layoutSubviews {
[super layoutSubviews];
self.imageView.bounds = CGRectMake(0,0,75,75);
self.imageView.frame = CGRectMake(0,0,75,75);
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
CGRect tmpFrame = self.textLabel.frame;
tmpFrame.origin.x = 77;
self.textLabel.frame = tmpFrame;
tmpFrame = self.detailTextLabel.frame;
tmpFrame.origin.x = 77;
self.detailTextLabel.frame = tmpFrame;
}
Run Code Online (Sandbox Code Playgroud)
Bro*_*oks 11
所以UITableViewCell的问题在于你无法控制内置对象的大小(即imageView,contentView,accessoryView,backgroundView).当表格更改时,您的自定义设置会被践踏.
正如Behlul指出的那样,您可以使用layoutSubviews强制大小正确,但问题是每次表滚动时都会调用layoutSubviews.这是很多不必要的重新布局调用.
另一种方法是将所有内容添加到contentView.同样,如果要自定义背景,则可以创建透明backgroundView并将自定义背景视图(例如myBackgroundView)添加为backgroundView的子视图.
通过这种方式,您可以根据需要放置和调整项目大小.
缺点是不再从附件或图像视图接收库存消息.你只需创建自己的.
希望有所帮助!
// This code is not tested
// MyCustomTableViewCell
- (id) init{
self = [super initWithStyle: UITableViewCellStyleDefault reuseIdentifier:@"MyReuseIdentifier"];
if(self){
//image view
my_image_view = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"default_image.png"]] retain];
[my_image_view setFrame:CGRectMake(10,10,30,30)];
[self.contentView addSubview:my_image_view];
//labels
my_text_label = [[[UILabel alloc] initWithFrame:CGRectMake(50,10,100,15)] retain];
[self.contentView addSubview:my_text_label];
//set font, etc
//detail label
my_detail_label = [[[UILabel alloc] initWithFrame:CGRectMake(50,25,100,15)] retain];
[self.contentView addSubview:my_detail_label];
//set font, etc
//accessory view
//Whatever you want to do here
//attach "accessoryButtonTapped" selector to button action
//background view
UIView* background_view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 50)] autorelease];
[background_view setBackgroundColor:[UIColor greenColor]];
background_view.layer.cornerRadius = 17;
background_view.layer.borderWidth = 3;
background_view.layer.borderColor = [UIColor whiteColor].CGColor;
[self setBackgroundView:[[[UIView alloc] init] autorelease]];
[self.backgroundView addSubview:background_view];
}
return self;
}
- (void) setLabelText: (NSString*) label_text{
[my_text_label setText:label_text];
}
- (void) setDetailText: (NSString*) detail_text{
[my_detail_label setText: detail_text];
}
- (void) accessoryButtonTapped{
//call table view delegate's accessoryButtonTappedForRowWithIndexPath method
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
21219 次 |
最近记录: |