iOS/Objective-C:确定编程创建的tableview中imageView的大小
问题描述:
我想读取自动创建的tableView中的imageView的宽度和高度,以便执行一些修改。iOS/Objective-C:确定编程创建的tableview中imageView的大小
但是,我通常使用的方法返回0的宽度可能是因为imageView没有明确定义。有没有人知道一种方法来做到这一点。
float imageWidth =cell.imageView.frame.size.width;
注:这是我正在创建实现代码如下:
-(void) makeTableView {
//origin,origin,width,height
_myTableView = [[UITableView alloc] initWithFrame:
CGRectMake(160, 114, 140, 100) style:UITableViewStylePlain];
_myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_myTableView.delegate = self;
_myTableView.dataSource = self;
_myTableView.scrollEnabled = YES;
_myTableView.layer.cornerRadius = 5;
_myTableView.hidden = YES;
_myTableView.backgroundColor = [UIColor grayColor];
_myTableView.rowHeight=24;
_myTableView.layer.borderColor=[UIColor grayColor].CGColor;
_myTableView.layer.borderWidth=1;
[self.view addSubview:_myTableView];
}
或许有此处设定的图像大小的方式,但我不知道它是什么。
或者,我可以将这些值设置为here,但似乎涉及子类tableviewcell或修改昂贵的上下文,也阻止我四舍五入图像。
编辑:我目前的cellForRowAtIndexPath
我似乎可以设置使用下面的代码的ImageView的大小。然而,通常情况下,下面的代码可以创建漂亮的圆形图像,而不管输入图片的大小如何 - 并非全部都是方形的。相反,一些图像出现圆形,但其他图像出来椭圆形。因此,一些没有设置帧大小后在这里工作......
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
if (tableView == myTableView) {
static NSString *myRowIdentifier = @"myRowIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier:myRowIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myRowIdentifier] ;
}
Items* item = [_items objectAtIndex:indexPath.row];
cell.textLabel.text = item.name;
NSString*pic = item.pic;
if (pic.length >=3) {
cell.imageView.image = [self loadImageNamed:pic];
}
else {
cell.imageView.image = [UIImage imageNamed:@"placeholder.png"];
}
//Because there is no custom cell, set size of imageview with following code
cell.imageView.frame = CGRectMake(0,
0,
24,
24);
cell.imageView.layer.cornerRadius = cell.imageView.frame.size.width/2;
cell.imageView.clipsToBounds = YES;
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
}
答
如果你仍然得到宽度= 0,记得使用
[cell layoutIfNeeded];
你能证明你的'cellForRowAtIndexPath'代码? –