2017-10-17 71 views
0

我想讀取自動創建的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

你能證明你的'cellForRowAtIndexPath'代碼? –

回答

0

如果你仍然得到寬度= 0,記得使用

[cell layoutIfNeeded];