Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当我在ios中点击按钮时,UITableView框架不适合self.view_Ios_Objective C_Uitableview - Fatal编程技术网

当我在ios中点击按钮时,UITableView框架不适合self.view

当我在ios中点击按钮时,UITableView框架不适合self.view,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我是iOS的初学者,在我的项目中,我创建了一个UITableView,我在这里为TableList添加了headerview,在headerview的内部,我添加了一个UILabel和一个UIbutton,好的 当我加载tableList时,我加载的是headerView“label”文本为空,当我点击添加到tableList headerView上的按钮时,我加载的是UIlabel文本,作为按钮操作中的内容 我的主要问题是,当我第一次加载tableList时,tableList和我下面的“第一

我是iOS的初学者,在我的项目中,我创建了一个UITableView,我在这里为TableList添加了headerview,在headerview的内部,我添加了一个UILabel和一个UIbutton,好的

当我加载tableList时,我加载的是headerView“label”文本为空,当我点击添加到tableList headerView上的按钮时,我加载的是UIlabel文本,作为按钮操作中的内容

我的主要问题是,当我第一次加载tableList时,tableList和我下面的“第一张图片”一样完美,当我点击按钮时,tableList和我的第二张图片不一样完美。为什么表格列表框架在变化

我的代码: firstimage:

第二张图片:

有一个最好的方法来做你想做的事

此方法
-(UIView*)tableView:(UITableView*)tableView view for headerinsection:(NSInteger)section
用于为每个节设置一个标题。我想你需要把它去掉

您希望所有的tableView都有一个标题。因此,创建一个自定义的
UIVIew
变量(
self.headerView
),您可以将其保留在ViewController中(当前在
viewForHeaderInSection
方法中创建的视图)

然后创建一个方法,比如更新标题标签的
upadateHeaderView
。在按钮操作方法中调用此方法(而不是重新创建tableView)

要在创建tableView时完成(仅一次),请调用
[tableView设置TableHeaderView:self.headerView]-(void)createTableList
方法中的code>。现在,所有tableView都有了一个标题

因此,您需要创建一个customView。下面是一个示例标题:

@interface CustomView : UIVIew

@property(nonatomic, strong) UILabel *label;
@property(nonatomic, strong) UIButton *button;

@end
您的viewController会变成这样:

#import "ViewController.h"

@interface ViewController (){

    UILabel *label;
    NSArray * mainArray;
    NSString * HeaderlabelText;
    UIView * ContentView;
    CustomView *headerView;
}

@end

@implementation ViewController {

}

- (void)viewDidLoad{
    [super viewDidLoad];
    [self createHeader];
    [self createTableList];
}

-(void)createTableList{

    MaintableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    MaintableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    MaintableView.dataSource=self;
    MaintableView.delegate=self;
    MaintableView.scrollEnabled = YES;
    [MaintableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    [MaintableView setTableHeaderView:self.headerView];
    [self.view addSubview:MaintableView];

    mainArray = [[NSArray alloc]initWithObjects:@"1",@"2",@"3", nil];

}

- (void)createHeaderView {
    self.headerView = [CustomView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 30.0f)];
    [self.headerView.button addTarget:self
                               action:@selector(PopOverAction:)
                     forControlEvents:UIControlEventTouchUpInside];
    //Setup your customView HERE
}

- (void)updateHeader {
    //Update self.headerView HERE
    [self.headerView.label setText:HeaderlabelText];
}
//Delegate methods:-

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return mainArray.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [MaintableView dequeueReusableCellWithIdentifier:CellIdentifier   forIndexPath:indexPath] ;

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }


    label = [[UILabel alloc]initWithFrame:CGRectMake(20, 10, 300, 30)];
    label.textColor = [UIColor blackColor];
    label.text = [mainArray objectAtIndex:indexPath.row];
    label.font = [UIFont systemFontOfSize:15.0];
    [cell.contentView addSubview:label];

    UIView *bgview = [[UIView alloc] init];
    bgview.backgroundColor = [UIColor darkGrayColor];
    cell.selectedBackgroundView = bgview;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
- (void)PopOverAction:(id)sender{

    HeaderlabelText = @"Section1";
    [self updateHeader];
}

您好@AbhiRam为什么点击后重新创建表格视图?因为我在点击按钮后分配了标题视图文本,更不用说重新创建我的表格列表标题视图文本了。如果有更好的方法,请告诉我您的表格视图中只有一个标题,或者每个部分只有一个标题?@TheRonin为什么会出现这个问题来吗?
#import "ViewController.h"

@interface ViewController (){

    UILabel *label;
    NSArray * mainArray;
    NSString * HeaderlabelText;
    UIView * ContentView;
    CustomView *headerView;
}

@end

@implementation ViewController {

}

- (void)viewDidLoad{
    [super viewDidLoad];
    [self createHeader];
    [self createTableList];
}

-(void)createTableList{

    MaintableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    MaintableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    MaintableView.dataSource=self;
    MaintableView.delegate=self;
    MaintableView.scrollEnabled = YES;
    [MaintableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    [MaintableView setTableHeaderView:self.headerView];
    [self.view addSubview:MaintableView];

    mainArray = [[NSArray alloc]initWithObjects:@"1",@"2",@"3", nil];

}

- (void)createHeaderView {
    self.headerView = [CustomView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 30.0f)];
    [self.headerView.button addTarget:self
                               action:@selector(PopOverAction:)
                     forControlEvents:UIControlEventTouchUpInside];
    //Setup your customView HERE
}

- (void)updateHeader {
    //Update self.headerView HERE
    [self.headerView.label setText:HeaderlabelText];
}
//Delegate methods:-

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return mainArray.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [MaintableView dequeueReusableCellWithIdentifier:CellIdentifier   forIndexPath:indexPath] ;

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }


    label = [[UILabel alloc]initWithFrame:CGRectMake(20, 10, 300, 30)];
    label.textColor = [UIColor blackColor];
    label.text = [mainArray objectAtIndex:indexPath.row];
    label.font = [UIFont systemFontOfSize:15.0];
    [cell.contentView addSubview:label];

    UIView *bgview = [[UIView alloc] init];
    bgview.backgroundColor = [UIColor darkGrayColor];
    cell.selectedBackgroundView = bgview;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}
- (void)PopOverAction:(id)sender{

    HeaderlabelText = @"Section1";
    [self updateHeader];
}