网格表视图中的Scrollview

时间:2010-07-23 06:02:02

标签: iphone objective-c

我对于iPhone开发来说是全新的,这就是我有关于如何在表格视图中实现滚动视图的查询的原因我使用以下代码

#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 =@"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 =@"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 =@"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 =@"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 =@"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设置滚动视图,那么它不起作用。

请提前致谢

1 个答案:

答案 0 :(得分:1)

我没有阅读您的代码,请重新格式化,以便其他人可以轻松阅读。

UITcView中的UIScrollView是什么意思?细胞里面?我仍然没有得到它。 FYI UITableView继承自UIScrollView ...

您确实想要实现哪些功能? 我建议您阅读Apple提供的一些示例。关于UIKit,有非常好的和广泛的例子。