【问题标题】:change width of a uitableviewcell更改 uitableviewcell 的宽度
【发布时间】:2010-08-23 19:09:31
【问题描述】:

我尝试更改 tableview 中单元格的 with,我不做自定义单元格,我只是子类 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];

但是我的单元格的宽度没有改变,为什么?

【问题讨论】:

  • 请注意消息格式(请参阅问题中的更改) - 每个代码行应以至少 4 个空格开头,以便看起来像一个代码(这些空格不会出现在最后发布)。

标签: iphone objective-c uitableview


【解决方案1】:

表格视图在添加到表格视图时会更改单元格的框架。如果你想改变一个单元格的宽度,你要么改变表格的宽度,要么改变单元格中contentView的宽度。

【讨论】:

  • 但为什么在苹果示例中他们这样做:TimeZoneCell *timeZoneCell = (TimeZoneCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (timeZoneCell == nil) { timeZoneCell = [[[TimeZoneCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; timeZoneCell.frame = CGRectMake(0.0, 0.0, 320.0, ROW_HEIGHT); }
  • 我不确定他们为什么这样做。这是不必要的代码,对单元格的外观没有影响。继续将 ROW_HEIGHT 更改为 500.0f,您会发现没有什么不同。完全删除该行,仍然没有任何变化。单元格本身的框架始终由表格确定大小。您只能可靠地更改其内容的框架。
  • 但是他们在通讯录应用中是怎么做的,我们看到左边是一张图片,右边是一个tableview,所以视图里有两个tablview?
  • 这只是一个自定义表格视图单元格。看看这篇文章。 developer.apple.com/iphone/library/documentation/userexperience/…
【解决方案2】:

如果要更改单元格的高度,请实现委托方法tableView:heightForRowAtIndexPath:

如果你想改变宽度,那么你应该只在视觉上改变它(为所有单元格的子视图设置透明背景,除了那些不应该的),或者,如果所有单元格都应该有相同的宽度,你可以改变正如@Jerry Jones 所建议的那样,整个表格视图的宽度......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-24
    • 1970-01-01
    • 2014-05-20
    • 1970-01-01
    相关资源
    最近更新 更多