2010-07-23 87 views
-3

我爲全新的iPhone開發,這是我有一個關於如何使用下面的代碼滾動查看網格TabelView

#import <UIKit/UIKit.h> 

@class ScrollViewViewController; 

@interface ScrollViewAppDelegate : NSObject <UIApplicationDelegate> { 
    UIWindow *window; 
    ScrollViewViewController *viewController; 
} 

@property (nonatomic, retain) IBOutlet UIWindow *window; 
@property (nonatomic, retain) IBOutlet ScrollViewViewController *viewController; 

@end 

實現表視圖即時滾動視圖的查詢的原因////// //////////////////

#import "ScrollViewAppDelegate.h" 
#import "ScrollViewViewController.h" 

@implementation ScrollViewAppDelegate 

@synthesize window; 
@synthesize viewController; 


- (void)applicationDidFinishLaunching:(UIApplication *)application {  

    // Override point for customization after app launch  
    [window addSubview:viewController.view]; 
    [window makeKeyAndVisible]; 
} 


- (void)dealloc { 
    [viewController release]; 
    [window release]; 
    [super dealloc]; 
} 


@end 

///// //////////////////////

#import <UIKit/UIKit.h> 


@interface MyTableCell : UITableViewCell { 

    NSMutableArray *columns; 
} 

- (void)addColumn:(CGFloat)position; 

@end 

/// ///////////////////////

#import "MyTableCell.h" 


@implementation MyTableCell 

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier { 
    if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) { 
     // Initialization code 
     columns = [NSMutableArray arrayWithCapacity:5]; 
     [columns retain]; 
    } 
    return self; 
} 


- (void)addColumn:(CGFloat)position { 
    [columns addObject:[NSNumber numberWithFloat:position]]; 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated { 

    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
} 

- (void)drawRect:(CGRect)rect { 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 

    // just match the color and size of the horizontal line 
    CGContextSetRGBStrokeColor(ctx, 0.5, 0.5, 0.5, 1.0); 
    CGContextSetLineWidth(ctx, 0.25); 

    for (int i = 0; i < [columns count]; i++) { 
     // get the position for the vertical line 
     CGFloat f = [((NSNumber*) [columns objectAtIndex:i]) floatValue]; 
     CGContextMoveToPoint(ctx, f, 0); 
     CGContextAddLineToPoint(ctx, f, self.bounds.size.height); 
    } 

    CGContextStrokePath(ctx); 

    [super drawRect:rect]; 
} 


- (void)dealloc { 
    [super dealloc]; 
    [columns dealloc]; 
} 


@end 

//////////////////// //

#import <UIKit/UIKit.h> 

@interface RootViewController : UITableViewController { 
} 

@end 

/////////////////

#import "RootViewController.h" 

#import "MyTableCell.h" 

@implementation RootViewController 


#define LABEL_TAG 1 
#define VALUE_TAG 2 
#define FIRST_CELL_IDENTIFIER @"TrailItemCell" 
#define SECOND_CELL_IDENTIFIER @"RegularCell" 


- (void)viewDidLoad { 
    // Add the following line if you want the list to be editable 
    // self.navigationItem.leftBarButtonItem = self.editButtonItem; 
    self.title = @"Grids!"; 

} 


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


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; 
    return 19; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    NSString *MyIdentifier = [NSString stringWithFormat:@"MyIdentifier %i", indexPath.row]; 

    MyTableCell *cell = (MyTableCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 

    if (cell == nil) { 
     cell = [[[MyTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; 

     UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0, 30.0, 
                  tableView.rowHeight)] autorelease]; 
     [cell addColumn:40]; 
     label.tag = LABEL_TAG; 
     label.font = [UIFont systemFontOfSize:12.0]; 
     label.text [email protected]"S.NO";// [NSString stringWithFormat:@"%d", indexPath.row]; 
     label.textAlignment = UITextAlignmentRight; 
     label.textColor = [UIColor redColor]; 
     label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | 
     UIViewAutoresizingFlexibleHeight; 
     [cell.contentView addSubview:label]; 

     label = [[[UILabel alloc] initWithFrame:CGRectMake(40.0, 0, 70.0, 
                  tableView.rowHeight)] autorelease]; 
     [cell addColumn:120]; 
     label.tag = VALUE_TAG; 
     label.font = [UIFont systemFontOfSize:12.0]; 
     // add some silly value 
     label.text [email protected]"Product ID";// [NSString stringWithFormat:@"%d", indexPath.row * 4]; 
     label.textAlignment = UITextAlignmentRight; 
     label.textColor = [UIColor blueColor]; 
     label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | 
     UIViewAutoresizingFlexibleHeight; 
     [cell.contentView addSubview:label]; 

     label = [[[UILabel alloc] initWithFrame:CGRectMake(134.0, 0, 70.0, 
                  tableView.rowHeight)] autorelease]; 
     [cell addColumn:220]; 
     label.tag = VALUE_TAG; 
     label.font = [UIFont systemFontOfSize:12.0]; 
     // add some silly value 
     label.text [email protected]"Product Name";// [NSString stringWithFormat:@"%d", indexPath.row * 4]; 
     label.textAlignment = UITextAlignmentRight; 
     label.textColor = [UIColor greenColor]; 
     label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | 
     UIViewAutoresizingFlexibleHeight; 
     [cell.contentView addSubview:label]; 

     label = [[[UILabel alloc] initWithFrame:CGRectMake(230.0, 0, 70.0, 
                  tableView.rowHeight)] autorelease]; 
     [cell addColumn:310]; 
     label.tag = VALUE_TAG; 
     label.font = [UIFont systemFontOfSize:12.0]; 
     // add some silly value 
     label.text [email protected]"Customer Name";// [NSString stringWithFormat:@"%d", indexPath.row * 4]; 
     label.textAlignment = UITextAlignmentRight; 
     label.textColor = [UIColor greenColor]; 
     label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | 
     UIViewAutoresizingFlexibleHeight; 
     [cell.contentView addSubview:label]; 

     label = [[[UILabel alloc] initWithFrame:CGRectMake(320.0, 0, 70.0, 
                  tableView.rowHeight)] autorelease]; 
     [cell addColumn:400]; 
     label.tag = VALUE_TAG; 
     label.font = [UIFont systemFontOfSize:12.0]; 
     // add some silly value 
     label.text [email protected]"Customer Product";// [NSString stringWithFormat:@"%d", indexPath.row * 4]; 
     label.textAlignment = UITextAlignmentRight; 
     label.textColor = [UIColor greenColor]; 
     label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | 
     UIViewAutoresizingFlexibleHeight; 
     [cell.contentView addSubview:label]; 


    } 

    return cell; 
} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Navigation logic 
} 


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

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

- (void)viewWillDisappear:(BOOL)animated { 
} 

- (void)viewDidDisappear:(BOOL)animated { 
} 


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


- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview 
    // Release anything that's not essential, such as cached data 
} 


- (void)dealloc { 
    [super dealloc]; 
} 


@end 

////////////

#import <UIKit/UIKit.h> 

@interface ScrollViewViewController : UIViewController<UIScrollViewDelegate> { 
} 
@end 

/////////////

#import "ScrollViewViewController.h" 

#import "RootViewController.h" 
@implementation ScrollViewViewController 



/* 
// The designated initializer. Override to perform setup that is required before the view is loaded. 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { 
     // Custom initialization 
    } 
    return self; 
} 
*/ 


// Implement loadView to create a view hierarchy programmatically, without using a nib. 
- (void)loadView { 

    RootViewController *RootViewControllerLink = [[RootViewController alloc]initWithNibName:@"RootViewController" bundle:nil]; 
    RootViewControllerLink.view.tag = 100; 
    /* UIImageView *imgView = [[[UIImageView alloc] initWithImage: 
          [UIImage imageNamed:@"winkler-gnu-blue.png"]] autorelease]; 
    imgView.tag = 100; 

*/ 
    UIScrollView *scrollView = [[[UIScrollView alloc] 
           initWithFrame:CGRectMake(0,0,320,480)] autorelease]; 
    scrollView.delegate = self; 
    scrollView.minimumZoomScale = 0.25; 
    scrollView.maximumZoomScale = 2; 
    scrollView.bounces = NO; 
    scrollView.showsHorizontalScrollIndicator = NO; 
    scrollView.showsVerticalScrollIndicator = NO; 
    scrollView.contentSize = RootViewControllerLink.view.frame.size; 
    scrollView.contentOffset = 
    CGPointMake((RootViewControllerLink.view.frame.size.width-320)/2, 
       (RootViewControllerLink.view.frame.size.height-480)/2); 
    [scrollView addSubview:RootViewControllerLink.view]; 
    self.view = scrollView; 


} 

/*- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { 
return [self.view viewWithTag:100]; 
} 


- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view 
{ 
    return YES; 
}// default returns YES 
- (BOOL)touchesShouldCancelInContentView:(UIView *)view 
{ 

    return YES; 
} 
*/ 
// not called if canCancelContentTouches is NO. default returns YES if view isn't UIControl 

/* 
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
} 
*/ 


/* 
// Override to allow orientations other than the default portrait orientation. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 
*/ 

- (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)viewDidUnload { 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
    [super dealloc]; 
} 

@end 

在上面的代碼中,如果我爲UIIMage設置滾動,那麼它的工作原理,但如果我爲RootViewController設置滾動視圖,那麼它不工作。

請幫助在此先感謝

+3

首先格式化您的問題,並且不要將您的所有代碼放在這裏。StackOverflow用於幫助您堅持不要代碼審查的地方。只輸入相關的代碼,並確切地告訴我們你想要達到的目標。 – lukya 2010-07-23 06:24:37

+0

@lukya:好的,這次我爲他做了。 @aman:請在下次格式化您的代碼 – vodkhang 2010-07-23 06:26:38

+0

天啊。這麼多的代碼......沒有人會讀所有的。請在你的問題中更簡潔。 – Nubsis 2010-07-23 06:52:25

回答

1

我沒看過你的代碼,請重新格式化,以便其他人可以輕鬆地閱讀它。

你是指在UITableView中的UIScrollView是什麼意思?在細胞內部?我仍然不明白。 僅供參考UITableView繼承自UIScrollView ...

您真正想要實現哪些功能? 我會推薦閱讀一些Apple給出的樣本。關於UIKit有很多很好的例子。

+0

其實我想在表視圖中水平scrollView – AmanGupta007 2010-07-24 05:47:57