【问题标题】:Is it possible to make uitableheader passes just over the first tablecell?是否可以在第一个表格单元格上进行 uitableheader 传递?
【发布时间】:2011-04-27 09:38:22
【问题描述】:

我有一个带有 tableHeaderView 的简单 uitableview。我有不止一个部分。

我将 headerview 的高度设置为 30。但我想在我的 header 中使用 40 像素高度的 uiview(从 0,0 到 320,30 不透明。从 30,10 到 320,40 是透明的)。我在里面放了一张照片。点(0x30)上有一个小图标。它的高度= 10。 我只想在第一个表格单元格上显示我的 headerview。

您将在下面看到我将标题的高度设置为 30,但我在 viewForHeaderInSection 中创建了 40 像素高度的视图。

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {


    UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
    sectionView.backgroundColor = [UIColor redColor];
    [sectionView autorelease];
    return sectionView;

}

有可能吗?

【问题讨论】:

    标签: iphone uitableview header


    【解决方案1】:

    您的 sectionView 将受到标题高度的限制。即使您使用 1000 像素高的 UIView,它也只会在表格单元格/标题的高度定义的范围内显示。

    【讨论】:

    • 目前为止在我的知识中不可能,除非有人想出一个以前没有发现的扭曲的解决方法。
    【解决方案2】:

    也许我有你的问题的答案。我不知道这是否是一个好的解决方案,但我认为还可以。

    我创建了一个新的空 xib 文件并将 UITableViewCell 放入该文件中。我将 UITableViewCell 的高度更改为 54,并在 UITableViewCell 中放置了一个 UIView。视图的框架是 0,0,320,10。然后我将该 xib 文件的文件所有者更改为我的 TableViewController。

    @interface RootViewController : UITableViewController {
    
    UITableViewCell * firstCellInSection;
    
    }
    
    @property (nonatomic, retain) IBOutlet UITableViewCell * firstCellInSection;
    @end
    

    之后,我在 xib 文件中将 firstCellInSection Outlet 与我的 UITableViewCell 连接起来。

    然后我更改了 TableView 为所有单元格加载此 firstCell 的代码

    indexPath.row == 0
    

    代码如下:

    @synthesize firstCellInSection;
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    UITableViewCell * cell;
    if (indexPath.row == 0) {
        if (indexPath.row == 0) {
            static NSString *firstCellInSectionIdentifier = @"FirstCellInSectionIdentifier";
            cell = [tableView dequeueReusableCellWithIdentifier:firstCellInSectionIdentifier];
            if (cell == nil) {
                [[NSBundle mainBundle] loadNibNamed:@"FirstCell" owner:self options:nil];
                cell = firstCellInSection;
                self.firstCellInSection = nil;
            }
        }
    
    }
    
    else {
    
        static NSString *CellIdentifier = @"Cell";
    
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }
    
    }
    
    
    
    // Configure the cell.
    return cell;
    }
    

    自定义行高:

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) return 54;
    
    else return 44;
    }
    

    结合您的 headerView 代码,它可能看起来像您想要的那样。

    【讨论】:

    • 我还没有尝试过您的解决方案。根据您的解决方案,第一个单元格会比其他单元格大(10 像素)?
    • 我认为会有一个问题,如果用户向上滚动,第一个单元格消失,每个单元格都将相同。
    • 我不明白你的意思。我以为您只希望节标题有所不同。在我的代码中,每个部分都以 firstCellInSection 开头。当所有单元格都应该自定义时,您需要制作自己的 UITableViewCells。有不同的方法可以在文档中解释。编辑:我忘记了当有一个普通的 TableView 时,剖面视图仍然可见。在这种情况下它不起作用。
    • 又是什么原因小图标不能出现在sectionView中?
    • 我的问题是我没有用于部分的矩形标题。这就是为什么我在photoshop上拍了一张照片。图片尺寸为 320x40。图片右下角是透明的(从 10,30 到 320,40)。如果我能成功地在第一个单元格上制作 headerview,它将解决我的问题。当用户向上和向下滚动我的小左下角(标题视图中的 10x10)将始终留在那里。我认为 uitableview 是不可能的。
    猜你喜欢
    • 2014-07-31
    • 1970-01-01
    • 2014-11-07
    • 1970-01-01
    • 2011-06-03
    • 2020-09-13
    • 2020-07-28
    • 2015-08-03
    • 1970-01-01
    相关资源
    最近更新 更多