所以,我有一個自定義單元格級,並在執行我有這樣的代碼:如何在自定義單元格中移動CGRect?
- (void)layoutSubviews {
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x;
CGRect frame;
frame = CGRectMake(boundsX+10,10, 50, 50);
picture.frame = frame;
frame = CGRectMake(boundsX+75 ,0, 170, 50);
nameLabel.frame = frame;
frame = CGRectMake(boundsX+75 ,43, 225, 23);
costLabel.frame = frame;
frame = CGRectMake(boundsX+280, 30, 8, 13);
arrow.frame = frame;
}
現在讓我們看看這個佈局是如何工作的下面的圖片:
我很滿意其結果,但我也想根據nameLabel改變細胞costLabel位置。如果有兩個lines
在nameLabel文字,我不想改變costLabel位置,但是如果有隻有一個line
在nameLabel文字,我想移動costLabel上,所以我可以驅除掉的空間,使costLabel接近nameLabel。
我在的cellForRowAtIndexPath嘗試這樣做:
CGRect frame = cell.costLabel.frame;
frame.origin.y = frame.origin.y-10; // new y coordinate
cell.costLabel.frame = frame;
但沒有奏效。
任何解決方案或想法,我怎麼可以改變CGRect
座標(y原點)?
就可以計算出,如果文本將採取單個或多個線 –
我可以計算出有多少個字符?字符串,然後找出有多少行 – SmartTree
我猜@Owen Hartnett的答案應該這樣做 –