我有如下 4 种不同的场景。当用户单击 TableView 单元格服务器时,以前的状态显示在标题中,如下所示。我为长文本放置了动态水平滚动条。
我已经实现了以下代码
-(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/