2010-08-23 50 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所建议的整个表格视图的宽度...