2014-01-15 92 views
0

我试图根据标题UISegmentedControl中的选择调整部分标题的大小。 出于某种原因,它只是不想工作。我已经尝试过[self.tableView beginUpdates];[self.tableView endUpdates];之前,周围和之后的变化高度代码..但它只是很奇怪。调整UITableViewController中部分标题的大小无法正常工作

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

这是发生了什么: 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的更多:

是什么我做错了吗?

+0

你的Dropbox的链接似乎并不有效。 –

+0

对不起...假期项目..和度假村的无线网络连接不好。没有上传。我现在正在尝试。 –

+0

@TimothyMoose文件已上传,应该可以工作。感谢您的注意。 –

回答

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

编辑:

怎么样一个变量来跟踪高度?这是草率的代码,但如果概念的作品,你可以重构它:

// 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]; 
} 
+0

这确实奏效,但是...我喜欢动画,这是我试图完成的一部分。 –

+0

再试一次...看我上面的编辑 –

+0

我试过了......但不幸的是结果相同 –

相关问题