{"id":569,"date":"2018-02-23T23:40:06","date_gmt":"2018-02-23T15:40:06","guid":{"rendered":"http:\/\/60.205.208.74\/2018\/02\/23\/ios%e8%a1%a8%e6%a0%bc%e8%a7%86%e5%9b%be%ef%bc%88table-view%ef%bc%89\/"},"modified":"2018-02-23T23:40:06","modified_gmt":"2018-02-23T15:40:06","slug":"ios%e8%a1%a8%e6%a0%bc%e8%a7%86%e5%9b%be%ef%bc%88table-view%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.puzi7.net\/?p=569","title":{"rendered":"iOS\u8868\u683c\u89c6\u56fe\uff08Table View\uff09"},"content":{"rendered":"<span itemprop=\"description\"><div class=\"article-body\">\n<div class=\"article-intro\" id=\"content\">\n<h2>\u8868\u683c\u89c6\u56fe\u7684\u4f7f\u7528<\/h2>\n<p>IOS\u8868\u683c\u89c6\u56fe\u7531\u5355\u5143\u683c \uff08\u4e00\u822c\u53ef\u91cd\u590d\u4f7f\u7528\uff09 \u7ec4\u6210\uff0c\u7528\u4e8e\u663e\u793a\u5782\u76f4\u6eda\u52a8\u7684\u89c6\u56fe\u3002<\/p>\n<p>\u5728iOS \u4e2d,\u8868\u683c\u89c6\u56fe\u7528\u4e8e\u663e\u793a\u6570\u636e\u5217\u8868,\u5982\u8054\u7cfb\u4eba\u3001\u5f85\u529e\u4e8b\u9879\u6216\u8d2d\u7269\u9879\u5217\u8868\u3002<\/p>\n<h3>\u91cd\u8981\u7684\u5c5e\u6027<\/h3>\n<ul>\n<li>delegate<\/li>\n<li>dataSource<\/li>\n<li>rowHeight<\/li>\n<li>sectionFooterHeight<\/li>\n<li>sectionHeaderHeight<\/li>\n<li>separatorColor<\/li>\n<li>tableHeaderView<\/li>\n<li>tableFooterView<\/li>\n<\/ul>\n<h3>\u91cd\u8981\u7684\u65b9\u6cd5<\/h3>\n<pre>\n- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath\n<\/pre>\n<pre>\n- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths\n   withRowAnimation:(UITableViewRowAnimation)animation\n   <\/pre>\n<pre>\n- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier\n<\/pre>\n<pre>\n- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier\n   forIndexPath:(NSIndexPath *)indexPath\n   <\/pre>\n<pre>\n- (void)reloadData\n<\/pre>\n<pre>\n- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths\n   withRowAnimation:(UITableViewRowAnimation)animation\n   <\/pre>\n<pre>\n- (NSArray *)visibleCells\n<\/pre>\n<h3>\u793a\u4f8b\u4ee3\u7801\u548c\u6b65\u9aa4<\/h3>\n<p>1.\u5728ViewController.xib\u4e2d\u6dfb\u52a0\u8868\u683c\u89c6\u56fe\uff0c\u5982\u4e0b\u6240\u793a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.runoob.com\/wp-content\/uploads\/2013\/11\/tableViewInterface.jpg\">\n<\/p>\n<p>2. \u901a\u8fc7\u53f3\u952e\u5355\u51fb\u5e76\u9009\u62e9\u6570\u636e\u6e90\u548c\u59d4\u6258\u5c06\u59d4\u6258\u548c\u6570\u636e\u6e90\u8bbe\u5b9a\u5230&#8221;File&#8217;s Owner\uff08\u6587\u4ef6\u7684\u6240\u6709\u8005\uff09&#8221;\u3002\u8bbe\u7f6e\u6570\u636e\u6e90\u5982\u4e0b\u6240\u793a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.runoob.com\/wp-content\/uploads\/2013\/11\/tableViewInterfaceConnectDelegate.jpg\"><\/p>\n<p>3.\u4e3a\u8868\u683c\u89c6\u56fe\u521b\u5efaIBOutlet\u7684\u5e76\u5c06\u5176\u547d\u540d\u4e3amyTableView\u3002\u5982\u4ee5\u4e0b\u56fe\u7247\u4e2d\u6240\u793a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.runoob.com\/wp-content\/uploads\/2013\/11\/tableViewOutlet.jpg\"><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.runoob.com\/wp-content\/uploads\/2013\/11\/tableViewOutletSetname.jpg\"><\/p>\n<p>4. \u4e3a\u62e5\u6709\u6570\u636e\uff0c\u6dfb\u52a0\u4e00\u4e2aNSMutableArray\u4f7f\u5176\u80fd\u591f\u5728\u5217\u8868\u683c\u89c6\u56fe\u4e2d\u663e\u793a<\/p>\n<p>5.ViewController\u5e94\u91c7\u7528\u7684UITableViewDataSource\u548cUITableViewDelegate\u534f\u8bae\u3002ViewController.h\u4ee3\u7801\u5982\u4e0b\u6240\u793a<\/p>\n<pre>\n#import &lt;UIKit\/UIKit.h&gt;\n\n@interface ViewController : UIViewController&lt;UITableViewDataSource,\nUITableViewDelegate&gt;\n{\n    \n    IBOutlet UITableView *myTableView;\n    NSMutableArray *myData;\n}\n\n@end\n<\/pre>\n<p>6.\u6267\u884c\u6240\u9700\u7684\u8868\u683c\u89c6\u56fe\u59d4\u6258\u548c\u6570\u636e\u6e90\u7684\u65b9\u6cd5\u3002\u66f4\u65b0ViewController.m\uff0c\u5982\u4e0b\u6240\u793a<\/p>\n<pre>\n#import \"ViewController.h\"\n\n@interface ViewController ()\n\n@end\n\n@implementation ViewController\n\n- (void)viewDidLoad\n{\n    [super viewDidLoad];\n    \/\/ table view data is being set here\u00a0\u00a0\u00a0\u00a0\n    myData = [[NSMutableArray alloc]initWithObjects:\n    @\"Data 1 in array\",@\"Data 2 in array\",@\"Data 3 in array\",\n    @\"Data 4 in array\",@\"Data 5 in array\",@\"Data 5 in array\",\n    @\"Data 6 in array\",@\"Data 7 in array\",@\"Data 8 in array\",\n    @\"Data 9 in array\", nil];\n    \/\/ Do any additional setup after loading the view, typically from a nib.\n}\n\n- (void)didReceiveMemoryWarning\n{\n    [super didReceiveMemoryWarning];\n    \/\/ Dispose of any resources that can be recreated.\n}\n\n#pragma mark - Table View Data source \n- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:\n  (NSInteger)section{\n    return [myData count]\/2;\n}\n\n- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:\n  (NSIndexPath *)indexPath{\n    static NSString *cellIdentifier = @\"cellID\";\n    \n    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:\n    cellIdentifier];\n    if (cell == nil) {\n        cell = [[UITableViewCell alloc]initWithStyle:\n        UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];\n    }\n    NSString *stringForCell;\n    if (indexPath.section == 0) {\n        stringForCell= [myData objectAtIndex:indexPath.row];\n\n    }\n    else if (indexPath.section == 1){\n        stringForCell= [myData objectAtIndex:indexPath.row+ [myData count]\/2];\n\n    }\n    [cell.textLabel setText:stringForCell];\n    return cell;\n}\n\n\/\/ Default is 1 if not implemented\n- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{\n    return 2;\n}\n\n- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:\n  (NSInteger)section{\n    NSString *headerTitle;\n    if (section==0) {\n        headerTitle = @\"Section 1 Header\";\n    }\n    else{\n        headerTitle = @\"Section 2 Header\";\n\n    }\n    return headerTitle;\n}\n- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:\n  (NSInteger)section{\n    NSString *footerTitle;\n    if (section==0) {\n        footerTitle = @\"Section 1 Footer\";\n    }\n    else{\n        footerTitle = @\"Section 2 Footer\";\n        \n    }\n    return footerTitle;\n}\n\n#pragma mark - TableView delegate\n\n-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:\n (NSIndexPath *)indexPath{\n    [tableView deselectRowAtIndexPath:indexPath animated:YES];\n    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];\n    NSLog(@\"Section:%d Row:%d selected and its data is %@\",\n    indexPath.section,indexPath.row,cell.textLabel.text);\n}\n\n\n@end\n<\/pre>\n<p>7.\u73b0\u5728\u5f53\u6211\u4eec\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u65f6\u6211\u4eec\u5c31\u4f1a\u5f97\u5230\u4e0b\u9762\u7684\u8f93\u51fa<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.runoob.com\/wp-content\/uploads\/2013\/11\/tableviewOutput.jpg\"><\/p>\n<\/p><\/div>\n<\/p><\/div>\n<span>","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\"><span itemprop=\"description\">\u8868\u683c\u89c6\u56fe\u7684\u4f7f\u7528 IOS\u8868\u683c\u89c6\u56fe\u7531\u5355\u5143\u683c \uff08\u4e00\u822c\u53ef\u91cd\u590d\u4f7f\u7528\uff09 \u7ec4\u6210\uff0c\u7528\u4e8e\u663e\u793a\u5782\u76f4\u6eda\u52a8\u7684\u89c6\u56fe\u3002&hellip;<\/span><\/p>\n<p class=\"more-link-p btn-align-right\"><a class=\"green zoom-btn\" href=\"https:\/\/www.puzi7.net\/?p=569\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38,26],"tags":[74,93],"class_list":["post-569","post","type-post","status-publish","format-standard","hentry","category-ios","category-mobile","tag-ios","tag-mobile","zoom-theme-has-thumb"],"_links":{"self":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/569","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=569"}],"version-history":[{"count":0,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/569\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}