动态水平可滚动表视图标题

时间:2015-06-12 11:26:35

标签: ios objective-c uitableview

我有4种不同的场景如下。当用户单击表视图时,将调用单元服务器,并且先前的状态显示在与下面相同的类的标题中。我为长文本放置了动态水平滚动。

enter image description here

enter image description here

我已实施以下代码

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 600, 40)];

    UIButton *BtnBreadcrumb = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [BtnBreadcrumb addTarget:self action:@selector(selectBtnBreadcrumb:)
            forControlEvents:UIControlEventTouchUpInside];
    [BtnBreadcrumb setTitle:[self.subCategoryDict objectForKey:@"name"] forState:UIControlStateNormal];
    BtnBreadcrumb.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    BtnBreadcrumb.tintColor=TextColor;
    CGSize stringsize = [[self.subCategoryDict objectForKey:@"name"] sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:16.0f]}];
    BtnBreadcrumb.frame = CGRectMake(10, 5, stringsize.width, 40);
    [contentView addSubview:BtnBreadcrumb];

    CGSize labelSize;
    if (!ArayBreadcrumb || !ArayBreadcrumb.count){
    }else{

        NSMutableString* BreadcrumbName = [NSMutableString new];
        int expression=(int)[ArayBreadcrumb count];
        for(int i = 0; i < expression; i ++)
        {
            [BreadcrumbName appendFormat:@"/ %@", [ArayBreadcrumb objectAtIndex:i]];
        }

        //CGSize
        labelSize = [BreadcrumbName sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:16.0f]}];

        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(stringsize.width+10, 5, labelSize.width, 40)];
        label.font = [UIFont fontWithName:@"Helvetica" size:15.0];
        label.textColor = ThemeColor;
        [label setText:BreadcrumbName];
        [contentView addSubview:label];
     }

    [contentView setBackgroundColor:[UIColor colorWithRed:0.933f green:0.933f blue:0.933f alpha:1.00f]];

    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 600, 40)];
    scroll.contentSize = CGSizeMake(stringsize.width+10+ labelSize.width,scroll.frame.size.height);
   // scroll.contentSize = CGSizeMake(600, 40);
    scroll.showsHorizontalScrollIndicator = YES;
    [scroll addSubview:contentView];

    [self.view addSubview:scroll];

    return scroll;

}


-(void) selectBtnBreadcrumb:(id)sender{
  //  [self.navigationController popViewControllerAnimated:YES];

}

工作正常。现在我必须实现这一点,当用户点击BtnBreadcrumb中的NSMutableString* BreadcrumbName数据时,需要按照添加的顺序逐个删除,当它变为nill时,只需要popViewControllerAnimated:YES被称为

1 个答案:

答案 0 :(得分:1)

像一个简单的for循环和一个可变字符串就可以了。

加成

NSMutableString* breadCrumbName = [NSMutableString new];
int expression=(int)[ArayBreadcrumb count];
    for(int i = 0; i < expression; i ++)
    {
        [breadCrumbName appendFormat:@"/ %@", [ArayBreadcrumb objectAtIndex:i]];
    }

删除

NSMutableArray *components = [[str componentsSeparatedByString:@"/"] mutableCopy];
[components removeLastObject];
str = [[components componentsJoinedByString:@"/"] mutableCopy];

注意:组件中的最后一项将为空。还有更好的方法可以做到这一点,但这很快又很脏。