UITableView的创建及其一些常用方法

UITableView,它的数据源继承于UITableViewDataSource,它的委托UITableViewDelegate。

一、UITableView的创建

1.代码方式:

1     UITableView *tableView=[[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
2     tableView.backgroundColor=[UIColor grayColor];
3     [self.view addSubview:tableView];

 2.Main.storyboard方式:

二、TableView的实现

1 #import <UIKit/UIKit.h>
2 
3 @interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
4 
5 
6 @end
 1 #import "ViewController.h"
 2 
 3 @interface ViewController ()
 4 //为tableView提供数据
 5 @property(nonatomic,strong)NSDictionary *dic;
 6 @property(nonatomic,retain)NSArray *arr1;
 7 @property(nonatomic,retain)NSArray *arr2;
 8 @end
 9 
10 @implementation ViewController
11 
12 - (void)viewDidLoad {
13     [super viewDidLoad];
14     UITableView *tableView=[[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
15     // 设置tableView的委托
16     tableView.delegate=self;
17     // 设置tableView的数据源
18     tableView.dataSource=self;
19     // 设置tableView的背景颜色
20     tableView.backgroundColor=[UIColor greenColor];
21     [self.view addSubview:tableView];
22     _arr1=@[@"张三",@"李四"];
23     _arr2=@[@"刘老师",@"张老师"];
24     //为tableView提供数据
25     _dic=@{@"老师":_arr2,@"学生":_arr1};
26     
27     
28 }
29 //为UITableView划分分区
30 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
31 {
32     return [_dic count] ;
33 }
34 //设置每块分区的行数
35 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
36 {
37     
38     NSArray *arrKey=[_dic allKeys];
39     NSString *key=[arrKey objectAtIndex:section];
40     NSArray *arrObject=[_dic objectForKey:key];
41     return [arrObject count];
42 }
43 //为tableView的每个单元格提供数据
44 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
45 {
46     static NSString *cellId=@"cellId";
47     UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellId];
48     if(!cell){
49         cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
50     }
51     NSArray *arrKey=[_dic allKeys];
52     NSString *key=[arrKey objectAtIndex:indexPath.section];
53     NSArray *arrObject=[_dic objectForKey:key];
54     cell.textLabel.text=[arrObject objectAtIndex:indexPath.row];
55     return cell;
56 }
57 //隔行换色
58 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
59 {
60     if ([indexPath row] % 2 == 0) {
61         cell.backgroundColor = [UIColor blueColor];
62     } else {
63         cell.backgroundColor = [UIColor grayColor];
64     }
65 }
66 //为每块分区设置头高
67 -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
68 {
69     return  34;
70 }
71 //为每块分区设置头标题
72 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
73 {
74     NSArray *arr=[_dic allKeys];
75     return [arr objectAtIndex:section];
76 }

三、UITableViewDataSource的一些属性与方法

 1 @protocol UITableViewDataSource<NSObject>
 2 
 3 @required
 4 
 5 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
 6 
 7 // Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
 8 // Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
 9 
10 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
11 
12 @optional
13 
14 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;              // Default is 1 if not implemented
15 
16 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;    // fixed font style. use custom view (UILabel) if you want something different
17 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;
18 
19 // Editing
20 
21 // Individual rows can opt out of having the -editing property set for them. If not implemented, all rows are assumed to be editable.
22 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;
23 
24 // Moving/reordering
25 
26 // Allows the reorder accessory view to optionally be shown for a particular row. By default, the reorder control will be shown only if the datasource implements -tableView:moveRowAtIndexPath:toIndexPath:
27 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
28 
29 // Index
30 
31 - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;                                                    // return list of section titles to display in section index view (e.g. "ABCD...Z#")
32 - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index;  // tell table which section corresponds to section title/index (e.g. "B",1))
33 
34 // Data manipulation - insert and delete support
35 
36 // After a row has the minus or plus button invoked (based on the UITableViewCellEditingStyle for the cell), the dataSource must commit the change
37 // Not called for edit actions using UITableViewRowAction - the action's handler will be invoked instead
38 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
39 
40 // Data manipulation - reorder / moving support
41 
42 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath;
43 
44 @end

四、UITableViewDelegate的一些属性与方法

 1 @protocol UITableViewDelegate<NSObject, UIScrollViewDelegate>
 2 
 3 @optional
 4 
 5 // Display customization
 6 
 7 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
 8 - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
 9 - (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
10 - (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath NS_AVAILABLE_IOS(6_0);
11 - (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
12 - (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
13 
14 // Variable height support
15 
16 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
17 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
18 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
19 
20 // Use the estimatedHeight methods to quickly calcuate guessed values which will allow for fast load times of the table.
21 // If these methods are implemented, the above -tableView:heightForXXX calls will be deferred until views are ready to be displayed, so more expensive logic can be placed there.
22 - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(7_0);
23 - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0);
24 - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0);
25 
26 // Section header & footer information. Views are preferred over title should you decide to provide both
27 
28 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;   // custom view for header. will be adjusted to default or specified header height
29 - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;   // custom view for footer. will be adjusted to default or specified footer height
30 
31 // Accessories (disclosures). 
32 
33 - (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath NS_DEPRECATED_IOS(2_0, 3_0);
34 - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath;
35 
36 // Selection
37 
38 // -tableView:shouldHighlightRowAtIndexPath: is called when a touch comes down on a row. 
39 // Returning NO to that message halts the selection process and does not cause the currently selected row to lose its selected look while the touch is down.
40 - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
41 - (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
42 - (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
43 
44 // Called before the user changes the selection. Return a new indexPath, or nil, to change the proposed selection.
45 - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;
46 - (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
47 // Called after the user changes the selection.
48 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
49 - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
50 
51 // Editing
52 
53 // Allows customization of the editingStyle for a particular cell located at 'indexPath'. If not implemented, all editable cells will have UITableViewCellEditingStyleDelete set for them when the table has editing property set to YES.
54 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
55 - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
56 - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0); // supercedes -tableView:titleForDeleteConfirmationButtonForRowAtIndexPath: if return value is non-nil
57 
58 // Controls whether the background is indented while editing.  If not implemented, the default is YES.  This is unrelated to the indentation level below.  This method only applies to grouped style table views.
59 - (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath;
60 
61 // The willBegin/didEnd methods are called whenever the 'editing' property is automatically changed by the table (allowing insert/delete/move). This is done by a swipe activating a single row
62 - (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath;
63 - (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath;
64 
65 // Moving/reordering
66 
67 // Allows customization of the target row for a particular row as it is being moved/reordered
68 - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath;               
69 
70 // Indentation
71 
72 - (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath; // return 'depth' of row for hierarchies
73 
74 // Copy/Paste.  All three methods must be implemented by the delegate.
75 
76 - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(5_0);
77 - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender NS_AVAILABLE_IOS(5_0);
78 - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender NS_AVAILABLE_IOS(5_0);
79 
80 @end
原文地址:https://www.cnblogs.com/fmdxiangdui/p/5061959.html