UITableViewController中节标题的大小调整不正常

时间:2014-01-15 22:23:56

标签: ios objective-c uitableview uiviewcontroller

我正在尝试根据标题UISegmentedControl中的选择来调整节标题的大小。 出于某种原因,它只是不想工作。我在更改高度代码之前,之后和之后尝试过使用[self.tableView beginUpdates];[self.tableView endUpdates]; ..但它只是表现得很奇怪。

我让它隐藏并显示内容,但它似乎为视图分配了不同的高度,即使标题的大小应该更少。

这是发生的事情: https://dl.dropboxusercontent.com/u/3077127/Problem3.mov

这是我的代码:

typedef enum {
    kSearchTypeFrom = 0,
    kSearchTypeTo
} kSearchType;

@interface MainVC ()

@property (nonatomic, strong) FilterVC *filterView;
@property (nonatomic, assign) kSearchType searchType;

@end

@implementation MainVC

@synthesize filterView = _filterView;
@synthesize searchType = _searchType;

[...]

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    self.searchType = kSearchTypeFrom;

    [self.tableView beginUpdates];
    [self.tableView endUpdates];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    [cell.detailTextLabel setText:@"Test"];

    return cell;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (!self.filterView) {
        self.filterView = [[FilterVC alloc] init];
        [self.filterView.view setBackgroundColor:self.navigationController.navigationBar.barTintColor];
    }

    [self.filterView.segment setSelectedSegmentIndex:self.searchType];

    [self.filterView.segment addTarget:self action:@selector(didChangeSegmentSelection:) forControlEvents:UIControlEventValueChanged];

    return self.filterView.view;
}

- (float)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (self.searchType == kSearchTypeFrom)
    {
        return 130;
    }
    else {
        return 100;
    }
}

#pragma mark - Height change table section

- (void)didChangeSegmentSelection:(UISegmentedControl*)segment
{
    [self.tableView beginUpdates];

    self.searchType = segment.selectedSegmentIndex;

    NSLog(@"Selected: %d", segment.selectedSegmentIndex);

    if (segment.selectedSegmentIndex == 0)
    {
        [self.filterView.changeToText setHidden:NO];
        [self.filterView.changeToButton setHidden:NO];

        [self.filterView.fromButton setUserInteractionEnabled:NO];
    }
    else {
        [self.filterView.changeToText setHidden:YES];
        [self.filterView.changeToButton setHidden:YES];

        [self.filterView.fromButton setUserInteractionEnabled:YES];
    }

    [self.tableView endUpdates];
    [self.filterView.view needsUpdateConstraints];
}

[...]

FilterVC类只不过是一个包含以下内容的UIViewController:

#import "InsetTextField.h"

@interface FilterVC : UIViewController

@property (nonatomic, strong) IBOutlet InsetTextField *amountField;
@property (nonatomic, strong) IBOutlet UISegmentedControl *segment;

@property (nonatomic, strong) IBOutlet UIButton *fromButton;
@property (nonatomic, strong) IBOutlet UILabel *changeToText;
@property (nonatomic, strong) IBOutlet UIButton *changeToButton;

@end

我做错了什么?

1 个答案:

答案 0 :(得分:0)

作为尝试动画的替代方法,您可以尝试使用以下方法重新加载表:

- (void)didChangeSegmentSelection:(UISegmentedControl*)segment
{
    //[self.tableView beginUpdates];

    self.searchType = segment.selectedSegmentIndex;

    NSLog(@"Selected: %d", segment.selectedSegmentIndex);

    if (segment.selectedSegmentIndex == 0)
    {
        [self.filterView.changeToText setHidden:NO];
        [self.filterView.changeToButton setHidden:NO];

        [self.filterView.fromButton setUserInteractionEnabled:NO];
    }
    else {
        [self.filterView.changeToText setHidden:YES];
        [self.filterView.changeToButton setHidden:YES];

        [self.filterView.fromButton setUserInteractionEnabled:YES];
    }

    //[self.tableView endUpdates];
    [self.tableView reloadData];
    [self.filterView.view needsUpdateConstraints];
}

编辑:

变量跟踪高度怎么样?这是邋code的代码,但如果这个概念有效,你可以重构它:

// Declare a variable
@proprty (strong, nonatomic) float headerHeight;

// Use that variable for defining the height
- (float)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (self.searchType == kSearchTypeFrom)
    {
        headerHeight = 130;
    }
    else {
        headerHeight = 100;
    }

    return headerHeight
}

然后更改该变量:

- (void)didChangeSegmentSelection:(UISegmentedControl*)segment
{
    [self.tableView beginUpdates];

    self.searchType = segment.selectedSegmentIndex;

    // Change the variable used for the header height
    if (self.searchType == kSearchTypeFrom)
    {
        headerHeight = 130;
    }
    else {
        headerHeight = 100;
    }

    NSLog(@"Selected: %d", segment.selectedSegmentIndex);

    if (segment.selectedSegmentIndex == 0)
    {
        [self.filterView.changeToText setHidden:NO];
        [self.filterView.changeToButton setHidden:NO];

        [self.filterView.fromButton setUserInteractionEnabled:NO];
    }
    else {
        [self.filterView.changeToText setHidden:YES];
        [self.filterView.changeToButton setHidden:YES];

        [self.filterView.fromButton setUserInteractionEnabled:YES];
    }

    [self.tableView endUpdates];
    [self.filterView.view needsUpdateConstraints];
}