【问题标题】:cell text for two dynamically sections两个动态部分的单元格文本
【发布时间】:2011-11-11 13:29:01
【问题描述】:

我有两个 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,我的 secondArray 中没有任何文本...

更新

也有这个方法

- (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;
}

【问题讨论】:

    标签: iphone uitableview ios5 tableview


    【解决方案1】:

    始终为您的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];
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多