0

I am trying to read the width and height of the imageView in a tableView created automatically in order to perform some modifications.

However, the method I normally use is returning 0 for the width perhaps because the imageView is not explicitly defined. Does anyone know a way to do this.

float imageWidth =cell.imageView.frame.size.width;

Note: this is how I am creating tableview:

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

}

Perhaps there is a way to set the image sizes here, but I don't know what it would be.

Alternatively, I could set these values as here but that seems to involve subclassing the tableviewcell or modifying the context which is expensive and also is preventing me from rounding the image.

Edit: my current cellforRowAtIndexPath

I seem to be able to set the size of the imageView using below code. Normally, however, the code below creates nice round images regardless of the size of the incoming pictures--not all are square. Instead, some images come out round but others come out oval. So something is not working here...

- (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;
}
user6631314
  • 1,751
  • 1
  • 13
  • 44

1 Answers1

0

If after setting frame size you still get width=0, recall to use

[cell layoutIfNeeded];
nasli
  • 46
  • 7