IOS Study Notes (9) tableView Basics

Source: Internet
Author: User

Previous: http://www.bkjia.com/kf/201301/185441.html
TableView is a rolling view divided into different parts. Each part is further divided into rows. Each row is an instance of the UITableViewCell class. You can embed images, text, and anything into tableView cells to customize their shapes, heights, groups, or more. These are defined in the UITableViewDataSource and UITableViewDelegate Protocols respectively. # Import <UIKit/UIKit. h> @ interface TableViewController: UIViewController @ property (monatomic, strong) UITableView * myTableView; @ end @ implementation TableViewController @ synthesize myTableView;-(void) viewDidLoad {[super viewDidLoad]; self. myTableView = [[UITableView alloc] initWithFrame: self. view. bounds style: UITableViewStylePlain]; [self. view addSubview: self. myTableView];} creates a blank TableView UITa. BleViewStylePlain creates a blank Table ViewUITableViewStyleGrouped with no background image to create a Table View with the border of the background image and the rounded corner group, similar to the Settings app. How to add data? Follow the UITableViewDelegate Protocol: // set the tableviewCell height-(void) viewDidLoad {[super viewDidLoad]; CGRect tableViewFrame = self. view. bounds; self. myTableView = [[UITableView alloc] initWithFrame: tableViewFrame style: UITableViewStylePlain]; self. myTableView. delegate = self; [self. view addSubview: self. myTableView];}-(void) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath {CGFloat Result = 20366f; if ([tableView isEqual: self. myTableView]) {result = 40366f;} return result;} the position of the cell in TableView is displayed by its index path. An index path is a combination of section and row indexes. The section index starts from scratch. Add data: to add data, you also need to follow the UITableViewDataSource protocol to add the code self. myTableView. dataSource = self; self. myTableView. autoresizingMask = custom | UIViewAutoresizingFlexibleHeight;-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView {NSInteger result = 0; if ([tableView isEqual: self. myTableView]) {result = 3;} return result;}-(NSInteger) tableView :( UITableView *) tableView numberOfRowsI NSection :( NSInteger) section {NSInteger result = 0; if ([tableView isEqual: self. myTableView]) {switch (section) {case 0: {result = 3; break;} case 1: {result = 5; break;} case 2: {result = 8; break ;}} return result;}-(UITableViewCell *) tableView :( UITableView *) tableView cellForAtIndexPath :( NSIndexPath *) indexPath {UITableViewCell * result = nil; if ([tableView isEqual: self. myTableView]) {static N SString * Outputs = @ "MyCells"; result = [tableView topology: TableViewCellIdentifier]; if (result = nil) {result = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: tableViewCellIdentifier];} result. textLabel. text = [NSString stringWithFormat: @ "Section % ld, Cell % ld", (long) indexPath. section, (long) indexPath. row];} return resu Lt;} receive and process Table view events-(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {if ([tableView isEqual: self. myTableView]) {NSLog (@ "% @", [NSString stringWithFormat: @ "Cell % ld in Section % ld is Selected", (long) indexPath. row, (long) indexPath. section]) ;}} use different types of attachments in TableView. accessoryType = UITableViewCellAccessoryDetailDisclosureButton; create a custom tableView cell attachment-(UITabl EViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {UITableViewCell * result = nil; static NSString * myCellIdentifier = @ "SimpleCell"; result = [tableView progress: myCellIdentifier]; if (result = nil) {result = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: myCellIdentifier];} result. textLabel. t Ext = [NSString stringWithFormat: @ "Section % ld, Cell % ld", (long) indexPath. section, (long) indexPath. row]; UIButton * button = [UIButton buttonWithType: UIButtonWithType: UIButtonTypeRoundedRect]; button. frame = CGRectMake (0.0f, 0.0f, 150366f, 25366f); [button setTitle: @ "Expand" forState: UIControlStateNormal]; [button addTarget: self action: @ selector (export Mexico and :) forControlEvents: UIControlEventTouchUpInside ]; Result. accessoryView = button; return result;}-(void) specify mexpand :( id) paramSender {/* Take an at ion here */UITableViewCell * ownerCell = (UITableViewCell *) paramSender: superview; if (ownerCell! = Nil) {NSIndexPath * ownerCellIndexPath = [self. myTableView indexPathForCell: ownerCell]; NSLog (@ "Accessory in index path is tapped. indexPath = % @ ", ownerCellIndexPath); if (ownerCellIndexPath. section = 0 & ownerCellIndexPath. row = 1) {/* This is the second row in the first section */}}}

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.