开发者

How to set footer in one section of TableView manually (iOS)?

I would like to implement some code, which changes footer text in one section of the tableView (in viewDidAppear or viewWillAppear method). But how can I do it?

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

doesn't fit my requirements (It changes only once, during load of the tableVi开发者_开发知识库ew, but I need to change the footer's text after text in tableView cell is changed.


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


  1. Implement viewForFooterInSection and add your textField there. Also make that textField a property.

  2. When you have finished editing you tableViewCells, implement the textFieldDidEndEditing method and assign necessary value to the textField of your footerView.

  3. Once your textField is set, use [tableView reloadData] to implement the viewForFooterInSection again and it should work now.

Edit:

If you want to change the title of the Footer section after editing the UITableViewCell,

  1. Set a global variable or use NSUserDefaults to indicate that tableViewCell has been edited.

  2. self.tableView reloadData right after edit.

  3. In the method titleForFooterInSection check for that variable (this would mean that tableView has been edited) and set the title accordingly.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜