首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将文本放在表头右侧

如何将文本放在表头右侧
EN

Stack Overflow用户
提问于 2012-01-26 12:27:49
回答 2查看 298关注 0票数 1

在UITableView中,我们有头

例如

假设我想要根据企业所在的建筑对企业进行分组。

BuildingA 10 Business1 Business2 Business3 Business4

我如何排列使10在标题的右侧

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-01-26 12:33:20

您可以覆盖tableView:viewForHeaderInSection:以添加您自己的额外标签,该标签的矩形位于右侧某处,如CGRectMake(280,5,30,20);。您还应该设置它,以便文本右对齐。

请注意,如果这样做,还必须提供左侧标签(因为它已经不在那里了),并且还必须覆盖tableView:heightForHeaderInSection:

票数 1
EN

Stack Overflow用户

发布于 2012-01-27 21:31:30

因为用户可以使用自定义标题单元格,然后该单元格附加在标题中。

为此,实现了以下代码。

代码语言:javascript
复制
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 5;
}

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

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *v=[[[NSBundle mainBundle] loadNibNamed:@"tHeader" owner:self options:nil] objectAtIndex:0];
    [(UILabel*)[v viewWithTag:1] setText:[NSString stringWithFormat:@"%i",section+1]];
    return v;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return (section==0)?5:((section==1)?7:((section==2)?3:4));
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.text=[NSString stringWithFormat:@"       Hello-%i",indexPath.row];

    // Configure the cell.
    return cell;
}

download source from here.

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9013942

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档