两个动态部分的单元格文本

发布于 2024-12-14 15:34:46 字数 2623 浏览 5 评论 0原文

我有两个 tableView 部分,有时第一部分中没有任何数据,因此第二个部分现在位于第一个部分。

在我的 numberOfSectionsInTableView 方法中,我正在计算我的第一个数组:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if (appDelegate.firstArray.count == 0) {
    numberOfSections = 1;
} else {
    numberOfSections = 2;
}
return self.numberOfSections;

numberOfSections,它是我的头文件中的 int

这是我为单元格配置文本的方法

switch (indexPath.section) {
    case 0:
    {
        MyClass *inFirstArray = [appDelegate.firstArray objectAtIndex:indexPath.row];
        cell.detailTextLabel.text = inFirstArray.title;
        break;
    }
    case 1:
    {
        Event *inSecondArray = [appDelegate.secondArray objectAtIndex:indexPath.row];
        cell.detailTextLabel.text = inSecondArray.title;
        break;    

    default:
        break;
    }

}

问题是,如果我的firstArray计数等于0,我的第二个数组中没有任何文本...

更新

也有这个方法

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{

    //I'm realeasing it into dealloc
    sectionTitles = [[NSMutableArray alloc] init];

    [sectionTitles addObject:@"first ArrayTitle"];
    [sectionTitles addObject:@"second ArrayTitle"];
    NSString *sectionText = [sectionTitles objectAtIndex:section];
    return sectionText;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    //Adding a custom background image for table section header
    NSString *title = [times objectAtIndex:section];
    UIView *customView = [ [ [UIView alloc] initWithFrame: CGRectMake(0.0, -5.0, 320.0, 25.0) ] autorelease ];
    [customView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"tableBackground.png"]]];
    UIButton *button = [ [ [ UIButton alloc ] initWithFrame: CGRectMake(-9.0, 5.0, 320, 27.0) ] autorelease ];
    button.tag = section;
    [button setImage:[UIImage imageNamed:@"header_section_background.png" ] forState: UIControlStateNormal ];
    button.highlighted = NO;
    button.adjustsImageWhenHighlighted = NO;
    UILabel *label = [ [ UILabel alloc ] initWithFrame:CGRectMake(15.0, 5.0, 45, 22.0) ];
    label.alpha = 1.0;
    label.backgroundColor = [UIColor clearColor];

    label.text = title;
    label.textAlignment = UITextAlignmentRight;

    [customView addSubview: button ];
    [customView addSubview:label ];
    [customView sendSubviewToBack:button];

    return customView;
}

I have two tableView sections, sometimes the first section dosen't have any data in it so the second one now comes the first.

In my numberOfSectionsInTableView method I'm counting my first array:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if (appDelegate.firstArray.count == 0) {
    numberOfSections = 1;
} else {
    numberOfSections = 2;
}
return self.numberOfSections;

numberOfSections its an int into my header file.

and here's how I configure my text for cells

switch (indexPath.section) {
    case 0:
    {
        MyClass *inFirstArray = [appDelegate.firstArray objectAtIndex:indexPath.row];
        cell.detailTextLabel.text = inFirstArray.title;
        break;
    }
    case 1:
    {
        Event *inSecondArray = [appDelegate.secondArray objectAtIndex:indexPath.row];
        cell.detailTextLabel.text = inSecondArray.title;
        break;    

    default:
        break;
    }

}

The problem is that if my firstArray count equals to 0, I don't have any text into my secondArray...

UPDATE

Also have this methods

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{

    //I'm realeasing it into dealloc
    sectionTitles = [[NSMutableArray alloc] init];

    [sectionTitles addObject:@"first ArrayTitle"];
    [sectionTitles addObject:@"second ArrayTitle"];
    NSString *sectionText = [sectionTitles objectAtIndex:section];
    return sectionText;
}

and

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    //Adding a custom background image for table section header
    NSString *title = [times objectAtIndex:section];
    UIView *customView = [ [ [UIView alloc] initWithFrame: CGRectMake(0.0, -5.0, 320.0, 25.0) ] autorelease ];
    [customView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"tableBackground.png"]]];
    UIButton *button = [ [ [ UIButton alloc ] initWithFrame: CGRectMake(-9.0, 5.0, 320, 27.0) ] autorelease ];
    button.tag = section;
    [button setImage:[UIImage imageNamed:@"header_section_background.png" ] forState: UIControlStateNormal ];
    button.highlighted = NO;
    button.adjustsImageWhenHighlighted = NO;
    UILabel *label = [ [ UILabel alloc ] initWithFrame:CGRectMake(15.0, 5.0, 45, 22.0) ];
    label.alpha = 1.0;
    label.backgroundColor = [UIColor clearColor];

    label.text = title;
    label.textAlignment = UITextAlignmentRight;

    [customView addSubview: button ];
    [customView addSubview:label ];
    [customView sendSubviewToBack:button];

    return customView;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝海似她心 2024-12-21 15:34:46

始终为 numberOfSections 返回 2。对于您的 numberOfRowsInSection,执行以下操作:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];    
    if (section == 0)
        return [appDelegate.firstArray count];
    else
        return [appDelegate.secondArray count];
}

因此您的两个部分将始终存在,但如果没有数据,则行数为零。

Always return 2 for your numberOfSections. For your numberOfRowsInSection, do the following:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];    
    if (section == 0)
        return [appDelegate.firstArray count];
    else
        return [appDelegate.secondArray count];
}

So your two sections will always exist, but will have zero rows if there is no data for them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文