【问题标题】:How to set footer in one section of TableView manually (iOS)?如何在 TableView 的一个部分手动设置页脚(iOS)?
【发布时间】:2011-10-12 19:09:31
【问题描述】:

我想实现一些代码,它会更改 tableView 的一部分中的页脚文本(在 viewDidAppearviewWillAppear 方法中)。但是我该怎么做呢?

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section

不符合我的要求(它只更改一次,在加载 tableView 期间,但我需要在更改 tableView 单元格中的文本后更改页脚的文本。

【问题讨论】:

    标签: objective-c uitableview footer


    【解决方案1】:
        -(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    
            return 120;
    
        }
    
    
    
        -(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
            if (section == 0) {
                return @"Things We'll Learn";
            } else {
                return @"Things Already Covered";
            }
        }
    
    
    
    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
    
        [tableView reloadData];
    }
    

    【讨论】:

    • 谢谢。它仅在我使用本示例中的静态字符串时才有效。如果我在 if 子句中使用这样的代码: NSString *str = [NSString stringWithFormat:@"There are %d symbols", [[[[tableView cellForRowAtIndexPath:tappedRow] textLabel] text] length]];返回str;
    • return [NSString stringWithFormat:@"%@",[firstsectionarray objectAtIndex:indexPath.row]].like this for all section and care about numberofrowsinsection function
    • 什么是 firstSectionArray ?我没有部分的数组。
    • 如果我知道节和行号,如何在这个单元格中获取 textLabel ??
    • @LIAL UITableViewCell *myCell=[tableView cellForRowAtIndexPath:indexPath]; // myCell.textLabel
    【解决方案2】:
    1. 实现viewForFooterInSection 并在此处添加您的textField。还要将该 textField 设为属性。

    2. 完成 tableViewCells 的编辑后,实现 textFieldDidEndEditing 方法并为 footerView 的 textField 分配必要的值。

    3. 设置好 textField 后,使用 [tableView reloadData] 再次实现 viewForFooterInSection,它现在应该可以工作了。

    编辑:

    如果你想在编辑 UITableViewCell 后更改 Footer 部分的标题,

    1. 设置全局变量或使用NSUserDefaults表示tableViewCell已被编辑。

    2. self.tableView reloadData 编辑后。

    3. titleForFooterInSection 方法中检查该变量(这意味着 tableView 已被编辑)并相应地设置标题。

    【讨论】:

    • viewFoFooterInSection 或 titleForFooterInSection ??我应该按照您的建议将 textField 添加为 Footer 的子视图吗??
    • 你想改变页脚标题还是改变footerView中的textField?
    • 当我的单元格的 TableView 文本发生变化时,我想在此单元格的页脚中显示符号数量
    • 您想更改页脚的标题还是在页脚视图中嵌入一个文本字段并进行更改?
    • 我没有在页脚中嵌入任何文本字段,我只是想更改常规文本
    【解决方案3】:
    - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
    {
        if(section == 1)
        {
            // For Lable
            UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 40)] autorelease];
            tableView.sectionHeaderHeight = view.frame.size.height;
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, view.frame.size.width - 20, 44)];
            label.text = [self tableView:tableView titleForHeaderInSection:section];
            label.font = [UIFont boldSystemFontOfSize:16.0];
            label.shadowOffset = CGSizeMake(0, 1);
            label.backgroundColor = [UIColor clearColor];
            label.textColor = [UIColor whiteColor];
            label.adjustsFontSizeToFitWidth = YES;
            [label setLineBreakMode:NSLineBreakByTruncatingTail];
            [label setNumberOfLines:0];
            label.text = @“Your Text Here…..your Text Here”;
    
            [view addSubview:label];
            [label release];
            return view;
        }
    
        return nil;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
    {  
         if(section == 1)
        {
    
            return 60.0;
        }
    
        return 0;
    }
    

    【讨论】:

    • 还有4年多,你真的认为现在是现实吗?
    猜你喜欢
    • 2016-10-04
    • 2021-06-27
    • 1970-01-01
    • 1970-01-01
    • 2020-09-22
    • 1970-01-01
    • 1970-01-01
    • 2017-06-05
    • 1970-01-01
    相关资源
    最近更新 更多