UITableView code implementation instance for iPhone Development

Source: Internet
Author: User

IPhoneDevelopmentUITableViewThe code implementation example is described in this article.UITableViewThe basic operations are implemented by small instances. Let's look at the content.
 
1. Prerequisites

1.1UITableViewA UIView control must be loaded before you can add a UITableView.

1.2 The UITableView protocol <UITableViewDelegate, UITableViewDataSource>

The two protocols are as follows:

-- Number of rows:

 
 
  1. -(NSInteger) tableView :( UITableView *) table numberOfRowsInSection :( NSInteger) section
  2. {
  3. Return [tableArray count];
  4. }
  5.  
  6. -- Define rows
  7.  
  8. -(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath
  9. {
  10. Static NSString * SimpleTableIdentifier = @ "SimpleTableIdentifier ";
  11. UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:
  12. SimpleTableIdentifier];
  13. If (cell = nil)
  14. {
  15. // Default Style
  16. Cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault
  17. ReuseIdentifier: SimpleTableIdentifier] autoreler];
  18. }
  19. // Text settings
  20. NSUInteger row = [indexPath row];
  21. Cell. textLabel. text = [tableArray objectAtIndex: row];
  22. Return cell;
  23. }

2. Add UIView

 
 
  1. UIView * view = [[UIView alloc] initWithFrame: CGRectMake (0.0, 0.0, 320,260)];
  2. // Customize the recognition header and height
  3. View. autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight;
  4. View. backgroundColor = [UIColor blueColor];
  5. // Add the view to the main form
  6. [Self. view addSubview: view];

3. Add UITableView

 
 
  1. UITableView * table1 = [[UITableView alloc] initWithFrame: CGRectMake (0, 0.00, 300,250)];
  2. Table1.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight;
  3. Table1.backgroundColor = [UIColor redColor];
  4. // Add to view form
  5. [View addSubview: table1];
  6. Table1.delegate = self;
  7. Table1.dataSource = self;

4. Cancel click Animation

 
 
  1. [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

5. Scroll path identification until the specified position of the exponential continuous receiver on the screen

 
 
  1. - (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:
  2. (BOOL)animated Parameters   

IndexPath

The index path identifies the index in its row, and some of its indicator table view rows.

ScrollPosition

A constant that ends when the relative position of the row, center, and bottom is scrolled.

Animated

If you want the animation to change its position, it should not take effect immediately.

Instance code:

 
 
  1. Float heights = 216.0;
  2. CGRect frame = m_pTableView.frame;
  3. Frame. size = CGSizeMake (frame. size. width, frame. size. height-height );
  4. [UIView beginAnimations: @ "Curl" context: nil]; // starts the animation.
  5. [UIView setAnimationDuration: 0.30];
  6. [UIView setAnimationDelegate: self];
  7. [M_pTableView setFrame: frame];
  8. [M_pTableView scrollToRowAtIndexPath: selectedIndexPath atScrollPosition: UITableViewScrollPositionMiddle animated: YES];
  9. [UIView commitAnimations];

6. Obtain the selected columns ,:

The Code is as follows:

 
 
  1. For (NSInteger I = 0; I <[peopleTableView visibleCells] count]; I ++)
  2. {
  3. UITableViewCell * cell = [[peopleTableView visibleCells] objectAtIndex: I];
  4. If (cell. accessoryType = UITableViewCellAccessoryCheckmark) // Add the selected personnel to the list.
  5. {
  6. Item * item = (Item *) [NextPeopleList. SelectList objectAtIndex: I];
  7. [PeopleList addObject: item];
  8. IsCheck = TRUE;
  9. }
  10. }

7. The Cell will display the current event

 
 
  1. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3. if (indexPath.row % 2 == 0) {  
  4. cell.backgroundColor = DarkColor;  
  5. }else {  
  6. cell.backgroundColor = LightColor;  

Summary:IPhoneDevelopmentUITableViewThe content of the code implementation instance has been introduced. I hope this article will help you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.