2011-06-30 10 views
0

セクションヘッダーにUISliderがあります。スライダをスライドさせると、現在の表のセルが一種の「速度スクロール」メカニズムとして変更されます。iOS UITableView - セクションヘッダー内のUISliderはスクロール時にリセットされます

それは画面の中央(アイテム#7)のアイテムに当たったときに問題が発生します(選択したリストアイテムを再センタリングしていると仮定します)。そしてUISliderを最小。

ここでビューコントローラ内の関連するコードがあります:

// 
// ChapterSelectionView.m 
// TestApp 
// 
// Created by Darren Ehlers on 6/2/11. 
// Copyright 2011 __MyCompanyName__. All rights reserved. 
// 

#import "ChapterSelectionView.h" 
#import "ContentView.h" 

@implementation ChapterSelectionView 

@synthesize initSection; 
@synthesize initRow; 
@synthesize Book; 
@synthesize Chapter; 

@synthesize backButton; 
@synthesize contentView; 
@synthesize chapterList; 
@synthesize navBar; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) 
    { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)dealloc 
{ 
    [slider release]; 

    [super dealloc]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn"t have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren"t in use. 
} 

- (void) updateLabel:(id)sender 
{ 
    NSLog(@"slider.value=%f (%d)", slider.value, (int)(slider.value + 0.5)); 
    self.Chapter  = (int)(slider.value + 0.5); 
    navBar.topItem.title = [NSString stringWithFormat:@"%@ %d", self.Book.name, self.Chapter]; 

    [chapterList selectRowAtIndexPath:[NSIndexPath indexPathForRow:(self.Chapter - 1) inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle]; 
} 

-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [self.Book chapterCount]; 
} 

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    CGRect headerFrame = CGRectMake(0, 0, 320, 30); 

    UIView *headerView = [[[UIView alloc] initWithFrame:headerFrame] autorelease]; 

    UILabel *headerLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, headerView.frame.size.height)]; 
    UILabel *headerLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(headerView.frame.size.width - 30, 0, 30, headerView.frame.size.height)]; 

    CGRect frame = CGRectMake(40, 0, 240, 30); 

    slider = [[UISlider alloc] initWithFrame:frame]; 
    slider.minimumValue = 1.0; 
    slider.maximumValue = [self.Book chapterCount]; 
    slider.continuous = YES; 
    slider.value = self.Chapter; 

    [slider addTarget:self 
       action:@selector(updateLabel:) 
    forControlEvents:UIControlEventValueChanged]; 

    [headerView addSubview: slider]; 

    headerView.backgroundColor = [UIColor lightGrayColor]; 

    UIFont *helvetica = [UIFont fontWithName:@"Helvetica-Bold" size:14]; 
    headerLabel1.font = helvetica; 
    headerLabel1.text = @"1"; 
    headerLabel1.textColor  = [UIColor blackColor]; 
    headerLabel1.textAlignment = UITextAlignmentRight; 

    headerLabel1.opaque   = TRUE; 
    headerLabel1.backgroundColor = [UIColor lightGrayColor]; 

    [headerView addSubview:headerLabel1]; 
    [headerLabel1 release]; 

    headerLabel2.font = helvetica; 
    headerLabel2.text = [NSString stringWithFormat:@"%d", [self.Book chapterCount]]; 
    headerLabel2.textColor  = [UIColor blackColor]; 
    headerLabel2.textAlignment = UITextAlignmentLeft; 

    headerLabel2.opaque   = TRUE; 
    headerLabel2.backgroundColor = [UIColor lightGrayColor]; 

    [headerView addSubview:headerLabel2]; 
    [headerLabel2 release]; 

    return headerView; 
} 

-(float)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{ 
    return 30.0; 
} 

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myCellID]; 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:myCellID] autorelease]; 
    } 

    cell.backgroundColor = [UIColor whiteColor]; 

    UIFont *helvetica  = [UIFont fontWithName:@"Helvetica-Bold" size:14]; 
    cell.textLabel.font  = helvetica; 
    cell.textLabel.text  = [NSString stringWithFormat:@"Chapter %d", indexPath.row + 1]; 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [contentView changeCurrentChapter:(indexPath.row + 1)]; 

    [self.view removeFromSuperview]; 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Do any additional setup after loading the view from its nib. 

    navBar.topItem.title = [NSString stringWithFormat:@"%@ %d", self.Book.name, self.Chapter]; 
    chapterList.delegate = self; 

    [chapterList selectRowAtIndexPath:[NSIndexPath indexPathForRow:(self.Chapter - 1) inSection:0] animated:NO scrollPosition:UITableViewScrollPositionMiddle]; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

@end 

これは、テーブルビューには、選択した項目をrecentersときスライダーの「タッチ」が停止していることが表示されます。

考え、回避策などはありますか?

+0

現在のアイテムがリストを最初にスクロールさせるのは間違いありません。 UITableViewScrollPositionMiddleを使用すると、新しい現在のアイテムが#7にヒットしたときに発生します。 UITableViewScrollPositionTopを使用すると、すぐに起こります。 UITableViewScrollPositionBottomを使用すると、逆のことが起こります。 –

答えて

0

これで時間がかかり過ぎると、実際のデバイスで実行してしまいました。それはうまくいった...しゃっくりは起こらなかった。

これは、シミュレータでのみ発生するバグの1つです。したがって、それは今問題ではありません。

関連する問題