2

我想知道是否可以调整 UITableViewCell 的宽度。我想在它的左边放一张图片(比如地址簿中的联系人视图)。我在这里找到了一个帖子,它提供了我正在努力完成的事情的图片。我也想确认这篇文章的答案。

4

3 回答 3

2

OS3.0 在 API 中有一些样式选项可以满足您的需求。

或者,您可以在 Interface Builder 中创建一个完全自定义的表格视图单元格,例如在我的一个应用程序中,我在 cellForRowAtIndexPath 中执行此操作:

PlaceViewCell *cell = (PlaceViewCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [self createNewPlaceCellFromNib];
}

这是从NIB中挖掘出来的方法

- (PlaceViewCell*) createNewPlaceCellFromNib {
    NSArray* nibContents = [[NSBundle mainBundle]
                            loadNibNamed:@"PlaceCell" owner:self options:nil];
    NSEnumerator *nibEnumerator = [nibContents objectEnumerator];
    PlaceViewCell* placeCell = nil;
    NSObject* nibItem = nil;
    while ( (nibItem = [nibEnumerator nextObject]) != nil) {
        if ( [nibItem isKindOfClass: [PlaceViewCell class]]) {
            placeCell = (PlaceViewCell*) nibItem;
            if ([placeCell.reuseIdentifier isEqualToString: @"Place" ]) {
                //NSLog(@"PlaceCell - we have a winner!");
                break; // we have a winner
            } else {
                placeCell = nil;
                NSLog(@"PlaceCell is nil!");
            }
        }
    }
    return placeCell;
}

然后可以直接在 Interface Builder 中创建 UITableViewCell 子类。

于 2009-06-25T11:37:37.143 回答
0

IMO 不允许您更改 UITableViewCell 的宽度,但幸运的是您不需要这样做。Apple 提供了四种默认样式,它们能够在单元格的左边框显示一个图像(如果要将其放置在其他位置,则必须创建自定义布局)。

UITableViewCellStyle的四种样式分别是:

  • UITableViewCellStyleDefault
  • UITableViewCellStyleSubtitle
  • UITableViewCellStyleValue1
  • UITableViewCellStyleValue2

要将图像嵌入到 UITableViewCell 中,您应该阅读 Apples Developer Connection 网络上的 TableView 编程指南:使用预定义样式中的单元格对象

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    NSDictionary *item = (NSDictionary *)[self.content objectAtIndex:indexPath.row];
    cell.textLabel.text = [item objectForKey:@"mainTitleKey"];
    cell.detailTextLabel.text = [item objectForKey:@"secondaryTitleKey"];
    NSString *path = [[NSBundle mainBundle] pathForResource:[item objectForKey:@"imageKey"] ofType:@"png"];
    UIImage *theImage = [UIImage imageWithContentsOfFile:path];
    cell.imageView.image = theImage;
    return cell;
}
于 2009-06-25T15:00:12.973 回答
0

您不需要修改单元格的宽度。您真正需要的是使单元格看起来具有不同的宽度(例如,通过修改其可见子视图的宽度)。

不幸的是,您的链接不起作用,所以我不能更具体。

您也可以尝试查看协议中的tableView:indentationLevelForRowAtIndexPath:方法UITableViewDelegate

于 2012-03-23T19:20:03.837 回答