【问题标题】:how can i make a uitableview container to bounce within the view如何使 uitableview 容器在视图中反弹
【发布时间】:2011-07-25 23:09:33
【问题描述】:

好的,所以这很简单,但我希望我能清楚地解释这一点 - 我有一个表格视图,我想插入一个容器,然后让表格在到达顶部/底部时反弹。到目前为止,我能够将我的桌子放在一个容器中,但容器固定在视图上,而容器内的桌子会弹跳。同样,我正在寻找一种将桌子固定到容器上的方法,同时让容器弹跳。

这是我能够按照代码执行的操作:

我想要完成的是让黑匣子弹跳而不是其中的表格。

视图控制器中的我的 ViewDidLoad .m:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

//General View Setup
     UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"backgroundimage.png"]];
     self.view.backgroundColor = background;


//Table View Data 
    listOfItems = [[NSMutableArray alloc] init];
     NSArray *appleComputers = [NSArray arrayWithObjects:@"iPhone",@"iPod",@"MacBook",@"MacBook Pro",nil];
     NSDictionary *appleComputersDict = [NSDictionary dictionaryWithObject:appleComputers forKey:@"Computers"];
     NSArray *otherComputers = [NSArray arrayWithObjects:@"HP", @"Dell", @"Windows", @"Sony", @"Ivory", @"IBM", nil];
     NSDictionary *otherComputersDict = [NSDictionary dictionaryWithObject:otherComputers forKey:@"Computers"];
     [listOfItems addObject:appleComputersDict];
     [listOfItems addObject:otherComputersDict];
 self.navigationItem.title = @"Computers";

// Create a table
 tblSimpleTable.delegate = self;
 CGRect cgRct = CGRectMake(10, 50, 300, 300);           
 tblSimpleTable = [[UITableView alloc] initWithFrame:cgRct style:UITableViewStyleGrouped];  // Initilize the table
     [tblSimpleTable setBackgroundColor:[UIColor blackColor]];

 tblSimpleTable.sectionHeaderHeight = 30.0;
 tblSimpleTable.sectionFooterHeight = 30.0;
 tblSimpleTable.delegate = self;
 tblSimpleTable.dataSource = self;
 [self.view addSubview:tblSimpleTable]; 



//Create the header 
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 60)];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 300, 40)];
headerLabel.text = NSLocalizedString(@"Header for the table", @"");
headerLabel.textColor = [UIColor whiteColor];
headerLabel.shadowColor = [UIColor yellowColor];
headerLabel.shadowOffset = CGSizeMake(0, 1);
headerLabel.font = [UIFont boldSystemFontOfSize:22];
headerLabel.backgroundColor = [UIColor clearColor];
[containerView addSubview:headerLabel];
 self.tblSimpleTable.tableHeaderView = containerView;



}

【问题讨论】:

    标签: objective-c xcode uitableview uiviewcontroller uilabel


    【解决方案1】:

    你为什么不使用 UIScrollView。

    我已经测试了您的代码并完成了必要的更改。希望你喜欢。

    代码:

    (this is your .h file)
    
    #import <UIKit/UIKit.h>
    
    @interface tableScrollViewController : UIViewController 
          <UITableViewDelegate,UITableViewDataSource, UIScrollViewDelegate> {
    
        UITableView *tblSimpleTable;
        NSMutableArray *listOfItems;
        NSMutableArray *appleComputers,*otherComputers;
            UIScrollView *scrollView;
      }
    
     @end
    
    
     (this is your .m file)
    
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 600)];
    scrollView.delegate = self;
    scrollView.backgroundColor = [UIColor grayColor];
    scrollView.contentSize = CGSizeMake(300, 800);   
    
     appleComputers = [[NSMutableArray  alloc] init];   // I made it by my style
        [appleComputers addObject: @"iPhone"];
        [appleComputers addObject:@"iPod"];
        [appleComputers addObject:@"MacBook"];
        [appleComputers addObject:@"MacBook Pro"];
    
        otherComputers = [[NSMutableArray  alloc] init]; 
        [otherComputers addObject: @"HP"];
        [otherComputers addObject:@"Dell"];
        [otherComputers addObject:@"Windows"];
        [otherComputers addObject:@"Sony"];
        [otherComputers addObject:@"Ivory"];
        [otherComputers addObject:@"IBM"];
    
    self.navigationItem.title = @"Computers";
    
    // Create a table
    tblSimpleTable.delegate = self;
    CGRect cgRct = CGRectMake(10, 50, 300, 600);           
    tblSimpleTable = [[UITableView alloc] initWithFrame:cgRct 
                                     style:UITableViewStyleGrouped];  // Initilize the table
    [tblSimpleTable setBackgroundColor:[UIColor blackColor]];
    
    tblSimpleTable.sectionHeaderHeight = 30.0;
    tblSimpleTable.sectionFooterHeight = 30.0;
    tblSimpleTable.scrollEnabled = NO;
    tblSimpleTable.delegate = self;
    tblSimpleTable.dataSource = self;
    [scrollView addSubview:tblSimpleTable]; 
    
    self.view = scrollView;
    
    //Create the header 
    UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 60)];
    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 300, 40)];
    headerLabel.text = NSLocalizedString(@"Header for the table", @"");
    headerLabel.textColor = [UIColor whiteColor];
    headerLabel.shadowColor = [UIColor yellowColor];
    headerLabel.shadowOffset = CGSizeMake(0, 1);
    headerLabel.font = [UIFont boldSystemFontOfSize:22];
    headerLabel.backgroundColor = [UIColor clearColor];
    [containerView addSubview:headerLabel];
    tblSimpleTable.tableHeaderView = containerView;
    
    }
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    {
    return 2;
    }
    
    // Customize the number of rows in the table view.
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    {
    if(section == 0)
      return [appleComputers count];
    else if(section == 1)
        return [otherComputers count];
    }
    
     // Customize the appearance of table view cells.
     - (UITableViewCell *)tableView:(UITableView *)tableView   
                           cellForRowAtIndexPath:(NSIndexPath *)indexPath 
     {
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = (UITableViewCell *)[tableView 
                dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero 
                      reuseIdentifier:CellIdentifier] autorelease];
    }
    
    if(indexPath.section == 0)
            cell.text = [NSString stringWithFormat:@"%@“, 
                                             [appleComputers objectAtIndex:indexPath.row]];
    else if(indexPath.section == 1)
            cell.text = [NSString stringWithFormat:@"%@“,
                                            [otherComputers objectAtIndex:indexPath.row]];
    
    return cell;
     }
    
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:
                 (NSIndexPath *)indexPath 
     {
      // do whatever here
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-25
      • 2011-09-19
      • 2013-09-27
      • 1970-01-01
      • 1970-01-01
      • 2013-05-28
      • 2015-09-14
      相关资源
      最近更新 更多