2011-12-01 43 views
0

我想減少單元格的一個標籤的寬度在特定的indexPath.row,但它不工作.. 下面我添加了代碼,我正在使用iOs 5故事板。 任何人有其他方式可以在運行時更改標籤的大小?在ios 5 tableView中的自定義單元

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"name&email"]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 


} 
UILabel *nameLabel = (UILabel *)[cell viewWithTag:100]; 
UILabel *detail = (UILabel *)[cell viewWithTag:101]; 
if(indexPath.row == 0){ 
    nameLabel.text = @"Name : "; 
    detail.text = @""; 
}else if(indexPath.row == 1){ 
    nameLabel.text = @"Email : "; 
    detail.text = @""; 
}else if(indexPath.row == 2){ 
    nameLabel.text = @"Location Alerts"; 
    detail.text = @"Notifies people when they are close to a saved spot"; 
// here i am changing the width of lable 
    detail.frame = CGRectMake(0, 0, 100, 0); 
}else if(indexPath.row == 3){ 
    nameLabel.text = @"Share This App :"; 
    detail.text = @"(post to facebook/twitter)"; 
}else if(indexPath.row == 4){ 
    nameLabel.text = @"Review This App :"; 
    detail.text = @"(on iTunes)"; 
}else if(indexPath.row == 5){ 
    nameLabel.text = @"Get Help :"; 
    detail.text = @"(send an email to me)"; 
} 
// Configure the cell... 

return cell; 
} 

回答

0

我不是專家,但我看到你使用了兩個不同的標識符。

如果我是你,我會嘗試:

static NSString *CellIdentifier = @"name&email"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"name&email"]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 


    } 

好嗎?告訴我它是否有效:)

0

如果您在Storyboard中創建了自定義單元格,請檢查標識符是什麼並使用它。 標識符應該屬於Attributes Inspector。如果標識符單元格,那麼你的代碼應該是這樣的東西來獲得單元格。

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 

如果你有一個UILabel稱爲詳細要改變的寬度,你可以通過改變框架和重用以前幀起源尺寸高度改變大小

detail.frame = CGRectMake(detail.frame.origin.x, detail.frame.origin.y, newWidth,detail.frame.size.height); 

確保你傳遞detail.size.height的高度,以保持高度相同的(我注意到你的榜樣,該高度是0,這不會使其可見)。希望有幫助

相關問題