2014-09-24 39 views
0

我有一張表。表有一個簡單的自定義單元格。這個單元格中只有標籤。 這裏是cellForRow功能的代碼:無法更改UITableviewCell子類中的子視圖位置

MyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 

cell.myLabel.text = [NSString stringWithFormat:@"%ld", (long)indexPath.row]; 
cell.myLabel.frame = CGRectOffset(cell.myLabel.frame, 0, -20); 
cell.myLabel.backgroundColor = [UIColor redColor]; 

return cell; 

一切工作正常,除了這一點:

cell.myLabel.frame = CGRectOffset(cell.myLabel.frame, 0, -20); 

標籤不想動! 我在iOS8中發現了這個問題。

任何人的幫助,請:)

MyCell.m

回答

0

文件添加layoutSubviews方法,改變myLabel框架

- (void)layoutSubviews { 
// always try to set frame in layoutSubviews 
[super layoutSubviews]; 
self.myLabel.frame = CGRectOffset(self.myLabel.frame, 0, -20); 
} 

,並請修改這樣

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

static NSString *cellIdentifier; 
cellIdentifier = @"Cell"; 

MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

if (cell == nil) { 
    cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    cell.myLabel.backgroundColor = [UIColor redColor]; 
} 
cell.myLabel.text = [NSString stringWithFormat:@"%ld", (long)indexPath.row]; 

return cell; 

} 
代碼
+0

的感謝!它解決了我的問題,但部分解決了。如果我scrolldown,然後新的單元格出現與標籤,不動:( – 2014-09-25 15:59:20

+0

修改我的答案檢查一次 – 2014-09-25 16:47:45

+0

對不起,我需要使用故事板中的表格原型,因此使用... if(cell == nil) ......不適合我的情況 – 2014-09-30 16:06:37