2013-03-01 104 views
1

我有一些問題。我想在scrollview中顯示靜態和動態對象。我有IBOutlets,他們已連接。我想更改消息和標題,它們是在代碼中手動創建的。所有其他對象都是靜態的。我的代碼是在這裏:uiscrollview中的靜態和動態對象

.h文件中:

@interface MessageContentViewController(){ 
    IBOutlet UIImageView *avatarImageView; 
    IBOutlet UIScrollView *scrollView; 
    IBOutlet UIImageView *msgBoardImg; 
    IBOutlet UIImageView *topBarImg; 
    IBOutlet UIButton  *backBtn; 
    NSMutableArray *allMessages; 
} 

@property int indexOfMsg; 

@end 

.m文件:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.jpg"]]]; 

    [self.navigationController setNavigationBarHidden:YES animated:YES]; 
    [scrollView setContentSize:CGSizeMake(scrollView.frame.size.width * allMessages.count, 0)]; 
    [scrollView setPagingEnabled:YES]; 

    [scrollView scrollRectToVisible:CGRectMake(scrollView.frame.size.width * indexOfMsg, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height) animated:NO]; 
    for (int i = 0; i < allMessages.count; i++) { 

     UIView *tempSubView = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * i, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)]; 
     [tempSubView addSubview:topBarImg]; 
     [tempSubView addSubview:backBtn]; 
     [tempSubView addSubview:msgBoardImg]; 
     [tempSubView addSubview:avatarImageView]; 
     [scrollView addSubview:tempSubView]; 
    } 

    [scrollView setContentOffset: CGPointMake(scrollView.frame.size.width * indexOfMsg, 0)]; 

    CGRect currentViewFrame = CGRectMake(scrollView.frame.size.width * indexOfMsg, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height); 
    [scrollView scrollRectToVisible:currentViewFrame animated:NO]; 

} 

的.xib文件:

enter image description here

請幫助我。提前致謝。

回答

0

你應該實現滾動視圖的委託方法scrollViewDidEndDecelerating:,您可以在其中檢測滾動距離然後爲不同的滾動視圖不同的標題和消息。

+0

我很驚訝。 scrollview沒有顯示。 – 2013-03-01 05:25:46

+0

我用額外的視圖控制器(.xib)解決了這個問題。 – 2013-03-01 10:30:47