iphone - 不同部分的自定义分组 UITable 标题

标签 iphone ios uitableview

我对不同部分的多个部分标题有疑问。

第一节标题文本有以下代码。但是,在第二部分中,它显示与第一部分相同的标题文本。我可以知道如何修改代码以便我可以在第二个标题中显示其他文本吗?

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// create the parent view that will hold header Label
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 44.0)];

// create the button object
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
headerLabel.textColor = [UIColor whiteColor];
headerLabel.highlightedTextColor = [UIColor whiteColor];
headerLabel.font = [UIFont boldSystemFontOfSize:20];
headerLabel.frame = CGRectMake(10.0, 0.0, 300.0, 44.0);

// If you want to align the header text as centered
// headerLabel.frame = CGRectMake(150.0, 0.0, 300.0, 44.0);

headerLabel.text = @"section 1"; // i.e. array element
[customView addSubview:headerLabel];

return customView;
}

最佳答案

你必须为每个部分使用不同的自定义 View 。

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section  {
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 44.0)];
    UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    headerLabel.backgroundColor = [UIColor clearColor];
    headerLabel.opaque = NO;
    headerLabel.textColor = [UIColor whiteColor];
    headerLabel.highlightedTextColor = [UIColor whiteColor];
    headerLabel.font = [UIFont boldSystemFontOfSize:20];
    headerLabel.frame = CGRectMake(10.0, 0.0, 300.0, 44.0);

    // If you want to align the header text as centered
    // headerLabel.frame = CGRectMake(150.0, 0.0, 300.0, 44.0);
if(section == 1)
    headerLabel.text = @"section 1"; // i.e. array element
else if(section == 2)
    headerLabel.text = @"section 2"; // i.e. array element
else
    headerLabel.text = @"section 3";//and so on
    [customView addSubview:headerLabel];

    return customView;
 }

关于iphone - 不同部分的自定义分组 UITable 标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9597351/

相关文章:

iphone - UITableView:将一行移动到空白部分

iphone - 通过 IP 地址获取主机名

ios - 推送通知声音在 iOS8 上不起作用

objective-c - 设置 UITableView 部分边框颜色

uitableview - (iOS)为什么导航 Controller 中的第二个 'push' segue总是崩溃?

ios - `Unable to dequeue a cell with identifier MyCell`

ios - 奇怪的 SpriteKit 帧速率下降 - 怀疑挥之不去的 SKPhysicsBody 实例

iphone - NSUserDefaults 是否删除暂时不需要的 key ?

iphone - uiview 之上的 webView 无法识别触摸

arrays - 如何使用prepareForSegue从二维数组传递值