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/

相关文章:

ios - objective-c : dequeueReusableCellWithIdentifier:forIndexPath: returns exception

ios - 如何在 iOS 的 UIAlertcontroller 中显示 UITableview?

ios - UITableView过滤数据的索引不正确

iphone - 将静态谷歌地图加载到 UIImageView 中?

ios - 删除 iPhone OpenGraph SMS 消息需要按 "Tap to Load Preview"

锁定屏幕时 iOS 停止查找信标

iphone - IOS 低功耗蓝牙连接间隔

ios - 如何在后台使用iOS mosquitto

android - NativeBase - ReactNative - 下拉列表

ios - 使用平滑滚动提前加载