2010-08-23 73 views
2

我嘗試改變一個tableview中的單元格,我不做一個自定義的單元格,我只是子類uitableviewcell。改變一個uitableviewcell的寬度

這是類

@implementation customCell 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 

    if (self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier]) { 

     CGRect nouveauframe = CGRectMake(0.0, 0.0, 44,44); 

     self.frame = nouveauframe; 

    } 
    return self; 
} 

-(void)dealloc 
{ 

    [super dealloc]; 
} 

@end 

,這是當我在的cellForRowAtIndexPath創建我的手機:

static NSString *CellIdentifier = @"customCell"; 

customCell *cell = (customCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) { 
    cell = [[[customCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; 
} 

// Configure the cell... 

[[cell textLabel]setText:[[[[Mission singletonMission] getObjectDataHistoireEncours] objectAtIndex:indexPath.row] valueForKey:[[langue singletonLangue] valeurBalise: @"_nom_label"]]]; 

cell.detailTextLabel.text = [[[[Mission singletonMission] getObjectDataHistoireEncours] objectAtIndex:indexPath.row] valueForKey:[[langue singletonLangue] valeurBalise: @"_valeur"]]; 

cell.userInteractionEnabled = FALSE; 

retour = [cell retain]; 

,但我的單元格的寬度沒有變化,爲什麼?

+0

請注意消息格式(請參閱您問題中的更改) - 每個代碼行應以至少4個空格開頭,以便看起來像代碼(這些空間不會出現在最後的帖子中)。 – 2010-08-23 22:04:34

回答

4

表格視圖在將單元格添加到表格視圖時更改單元格框架。如果要更改單元格的寬度,則應該更改表格的寬度,或更改單元格中的contentView的寬度。

+0

但爲什麼在蘋果例如他們這樣做: TimeZoneCell * timeZoneCell =(TimeZoneCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if(timeZoneCell == nil){0} TimeZoneCell = [[[TimeZoneCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; timeZoneCell.frame = CGRectMake(0.0,0.0,320.0,ROW_HEIGHT); } – alex 2010-08-23 19:30:29

+0

我不知道他們爲什麼這樣做。這是不必要的代碼,並且對單元格的外觀沒有影響。繼續並將ROW_HEIGHT更改爲500.0f,您會注意到沒有任何區別。完全刪除該行,仍然沒有任何變化。 單元格框架本身的大小由表格決定。您只能可靠地更改其內容的框架。 – 2010-08-23 19:58:26

+0

但他們如何在聯繫人應用程序中執行操作,我們在左側看到一張圖片,在右側看到一張桌面視圖,因此視圖中有兩個tablview? – alex 2010-08-24 14:11:19

1

如果要更改單元格的高度,請執行代理方法tableView:heightForRowAtIndexPath:

如果你想改變寬度,那麼你應該只能直觀地改變它(爲所有單元格的子視圖設置透明背景,除非不應該是這樣),或者如果所有單元格應該具有相同的寬度,正如@Jerry Jones所建議的整個表格視圖的寬度...