UITableView tableFooterView没有出现

时间:2012-03-16 08:46:50

标签: iphone ios uitableview

我有一个UITableView我添加了tableFooterView,由于某种原因tableFooterView没有出现?

我如何添加tableFooterView

重新加载tableFooterView数据后,我在connectionDidFinishLoading方法中添加tableview

所以我做的是

[controls reloadData];

UIView *myFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
if(self.item.canRunOnDemand)
{
    UIButton *buttonRunWorkflow = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [buttonRunWorkflow addTarget:self action:@selector(runWorkflow:) forControlEvents:UIControlEventTouchDown];
    [buttonRunWorkflow setTitle:@"Run Now" forState:UIControlStateNormal];
    buttonRunWorkflow.frame = CGRectMake(15, 5, 290, 44); 
    buttonRunWorkflow.backgroundColor = [UIColor clearColor];
    [buttonRunWorkflow setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [myFooterView addSubview:buttonRunWorkflow];
}
if(item.canRunAlways)
{
    UILabel *canRunAlwaysLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 46, 100, 44)];
    canRunAlwaysLabel.backgroundColor = [UIColor clearColor];
    canRunAlwaysLabel.text = @"Run Always:";
    UISwitch *canRunAlways = [[UISwitch alloc] initWithFrame:CGRectMake(115, 56, 100, 44)];
    [canRunAlways addTarget:self action:@selector(canRunAlwaysChanged:) forControlEvents:UIControlEventValueChanged];
    [myFooterView addSubview:canRunAlways];
    [myFooterView addSubview:canRunAlwaysLabel];
    [canRunAlwaysLabel release];
    [canRunAlways release];
}

[myFooterView release];

[controls.tableFooterView addSubview:myFooterView];

页脚视图高度

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 100;
}

我也试过这个:

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if(section == [[fields objectAtIndex:section] count] - 1)
    {
        return 100;
    }
    else {
        return 0;
    }
}
-(UIView*) tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if(section == [[fields objectAtIndex:section] count] - 1)
    {
        UIView *myFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];
        if(self.item.canRunOnDemand)
        {
            UIButton *buttonRunWorkflow = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            [buttonRunWorkflow addTarget:self action:@selector(runWorkflow:) forControlEvents:UIControlEventTouchDown];
            [buttonRunWorkflow setTitle:@"Run Now" forState:UIControlStateNormal];
            buttonRunWorkflow.frame = CGRectMake(15, 5, 290, 44); 
            buttonRunWorkflow.backgroundColor = [UIColor clearColor];
            [buttonRunWorkflow setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [myFooterView addSubview:buttonRunWorkflow];
        }
        if(item.canRunAlways)
        {
            UILabel *canRunAlwaysLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 46, 100, 44)];
            canRunAlwaysLabel.backgroundColor = [UIColor clearColor];
            canRunAlwaysLabel.text = @"Run Always:";
            UISwitch *canRunAlways = [[UISwitch alloc] initWithFrame:CGRectMake(115, 56, 100, 44)];
            [canRunAlways addTarget:self action:@selector(canRunAlwaysChanged:) forControlEvents:UIControlEventValueChanged];
            [myFooterView addSubview:canRunAlways];
            [myFooterView addSubview:canRunAlwaysLabel];
            [canRunAlwaysLabel release];
            [canRunAlways release];
        }
        return myFooterView;
    }
    else {
        return nil;
    }
}

2 个答案:

答案 0 :(得分:7)

通过查看UITableView.h的标头文件,我们会看到属性声明tableFooterView,如下所示:

@property(nonatomic,retain) UIView *tableFooterView; // accessory view below content. default is nil. not to be confused with section footer

因此默认propertynil。这就是为什么你不能将另一个UIView添加到nil UIView。 你应该这样做:

controls.tableFooterView = myFooterView;

答案 1 :(得分:-3)

您是否尝试在表格视图中添加页脚视图? 如果您正在尝试使用此方法实现页脚视图

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

这可能会对你有所帮助。谢谢!!