ios - 动态水平可滚动表格 View 标题

标签 ios objective-c uitableview

我有如下 4 种不同的场景。当用户单击 TableView 单元格服务器时,以前的状态显示在标题中,如下所示。我为长文本放置了动态水平滚动条。

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];

}

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

最佳答案

像简单的 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];

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

关于ios - 动态水平可滚动表格 View 标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30801916/

相关文章:

ios - UITextfields 占位符字体未使用 HelveticaNeueLTPro-Roman 字体固定在水平中心位置

javascript - 将 panright 与 Hammer.js 一起使用时如何仅触发一次方法?

ios - 错误 0x1c8250 : double free memory in iphone

ios - 无法更改 UITableViewCell 的文本颜色

objective-c - 在 iOS 上使用 libssh2 反向端口转发

ios - 将文本对齐到 TableView 中单元格左上角的简单方法?

objective-c - 从 Swift 调用 Objective C setter 方法时“没有成员”

ios - 在 Xcode 崩溃报告中发现问题

ios - 如何使用 Core Data 制作索引列表 TableView ,iOS Dev

ios - iOS 13 中 Controller 呈现模态时的 UINavigationBar 高度