{"id":563,"date":"2014-04-09T21:05:33","date_gmt":"2014-04-09T13:05:33","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=563"},"modified":"2016-07-15T15:05:10","modified_gmt":"2016-07-15T07:05:10","slug":"%e7%94%a8xib%e5%88%9b%e5%bb%batableviewcell%ef%bc%8c%e7%86%9f%e6%82%89mvc%e6%a8%a1%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/project\/2014\/04\/09\/210533563.html","title":{"rendered":"\u7528xib\u521b\u5efaTableViewCell\uff0c\u719f\u6089MVC\u6a21\u578b"},"content":{"rendered":"<p><strong>1\u3001\u6548\u679c\u5c55\u793a<\/strong><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-564\" src=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2016\/07\/iOS-\u6a21\u62df\u5668\u5c4f\u5e55\u5feb\u7167\u201c2014\u5e744\u67089\u65e5-\u4e0b\u53487.30.26\u201d.png\" alt=\"iOS \u6a21\u62df\u5668\u5c4f\u5e55\u5feb\u7167\u201c2014\u5e744\u67089\u65e5 \u4e0b\u53487.30.26\u201d\" width=\"256\" height=\"384\" \/><\/p>\n<p><strong>2\u3001\u8bbe\u8ba1\u8bf4\u660e<\/strong><\/p>\n<p>2.0 \u51c6\u5907\u76f8\u5173\u5c55\u793a\u6570\u636e\uff0c\u7f16\u5199\u6210plist\u6587\u4ef6<br \/>\n2.1 \u5220\u9664\u4e4b\u524d\u7684\u63a7\u5236\u5668\uff0c\u65b0\u5efa\u4e00\u4e2a\u7c7bSATableViewController\uff0c\u7ee7\u627fUITableViewController\uff1b\uff08MVC\u4e2d\u7684Contrller\uff09<br \/>\n2.2 \u521b\u5efa\u4e00\u4e2axib\uff0c\u8bbe\u8ba1\u4e00\u4e2aUITableViewCell\u7684\u57fa\u672c\u6837\u5f0f\uff1b<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-565\" src=\"http:\/\/www.yusian.com\/blog\/wp-content\/uploads\/2016\/07\/QQ20140409-1@2x.png\" alt=\"QQ20140409-1@2x\" width=\"300\" height=\"112\" \/><\/p>\n<p>2.3 \u521b\u5efa\u4e00\u4e2a\u7c7bSACarCell\uff0c\u8be5\u7c7b\u7528\u6765\u52a0\u8f7dxib\uff0c\u5e76\u7ed9xib\u4e2d\u5404\u63a7\u4ef6\u8d4b\u503c\uff1b\uff08MVC\u4e2d\u7684View\uff09<br \/>\n2.4 \u521b\u5efa\u4e00\u4e2a\u7c7bSACar\uff0c\u8be5\u7c7b\u7528\u6765\u521b\u5efa\u6570\u636e\u6a21\u578b\uff1b\uff08MVC\u4e2d\u7684Model\uff09<br \/>\n2.5 \u63a7\u5236\u5668\u64cd\u4f5c\uff1a2.5.1 \u901a\u8fc7\u6570\u7ec4\u52a0\u8f7dplist\u6587\u4ef6\u4e2d\u7684\u6570\u636e\uff0c\u518d\u7528\u6a21\u578b\u5c06\u6570\u636e\u683c\u5f0f\u5316\uff1b<br \/>\n2.5.2 \u5b9e\u73b0DataSource\u534f\u8bae\u7684\u4e24\u4e2a\u65b9\u6cd5\uff1a\u8868\u683c\u884c\u6570\uff0c\u8868\u683cCell\u5185\u5bb9<br \/>\n2.6 \u6a21\u578b\u64cd\u4f5c\uff1a<br \/>\n2.6.1 \u521b\u5efa\u6570\u636e\u7684\u5404\u4e2a\u5c5e\u6027\uff1b<br \/>\n2.6.2 \u5b9e\u73b0\u4e00\u4e2a\u7c7b\u65b9\u6cd5\uff0c\u5c06plist\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u8f6c\u6362\u6210\u6a21\u578b\u6570\u636e\uff1b<br \/>\n2.7 \u89c6\u56fe\u64cd\u4f5c\uff1a<br \/>\n2.7.1 \u7c7b\u65b9\u6cd5\u52a0\u8f7dxib\u6587\u4ef6\u4e2d\u7684\u57fa\u672c\u89c6\u56fe<br \/>\n2.7.2 xib\u4e2d\u7684\u5404\u4e2aview\u62bd\u8c61\u6210\u5c5e\u6027<br \/>\n2.7.3 \u5c06\u6a21\u578b\u62bd\u8c61\u6210\u89c6\u56fe\u7684\u4e00\u6761\u5c5e\u6027<br \/>\n2.7.4 \u91cd\u5199\u6a21\u578b\u5c5e\u6027\u7684set\u65b9\u6cd5\uff0c\u8ba9\u89c6\u56fe\u7ed3\u5408\u6a21\u578b\u5c06\u6570\u636e\u81ea\u52a8\u5c55\u793a\uff1b<\/p>\n<p><strong>3\u3001\u5173\u952e\u4ee3\u7801<\/strong><!--more--><br \/>\n3.1 SACar.h<\/p>\n<pre lang=\"objc\" line=\"1\">\/\/\n\/\/  SACar.h\n\/\/  XibCell\n\/\/\n\/\/  Created by yusian on 14-4-9.\n\/\/  Copyright (c) 2014\u5e74 yusian. All rights reserved.\n\/\/\n\n#import &lt;Foundation\/Foundation.h&gt;\n@interface SACar : NSObject\n\n@property (nonatomic, copy) NSString *icon;\n@property (nonatomic, copy) NSString *title;\n@property (nonatomic, copy) NSString *source;\n@property (nonatomic, copy) NSString *price;\n\n- (id)initWithDict:(NSDictionary *)dict;\n+ (id)carWithDict:(NSDictionary *)dict;\n\n@end<\/pre>\n<p>3.2 SACar.m<\/p>\n<pre lang=\"objc\" line=\"1\">\/\/\n\/\/  SACar.m\n\/\/  XibCell\n\/\/\n\/\/  Created by yusian on 14-4-9.\n\/\/  Copyright (c) 2014\u5e74 yusian. All rights reserved.\n\/\/\n\n#import \"SACar.h\"\n\n@implementation SACar\n- (id)initWithDict:(NSDictionary *)dict\n{\n    if (self = [super init]) {\n        self.icon = dict[@\"Icon\"];\n        self.title = dict[@\"Title\"];\n        self.source = dict[@\"Source\"];\n        self.price = dict[@\"Price\"];\n    }\n    return self;\n}\n\n+ (id)carWithDict:(NSDictionary *)dict\n{\n    return [[self alloc] initWithDict:dict];\n}\n@end<\/pre>\n<p>3.3 SACarCell.h<\/p>\n<pre lang=\"objc\" line=\"1\">\/\/\n\/\/  SACarCell.h\n\/\/  XibCell\n\/\/\n\/\/  Created by yusian on 14-4-9.\n\/\/  Copyright (c) 2014\u5e74 yusian. All rights reserved.\n\/\/\n\n#import &lt;UIKit\/UIKit.h&gt;\n@class SACar;\n\n@interface SACarCell : UITableViewCell\n\n@property (nonatomic, weak) IBOutlet UILabel *titleLable;\n@property (nonatomic, weak) IBOutlet UILabel *priceLable;\n@property (nonatomic, weak) IBOutlet UILabel *sourceLable;\n@property (nonatomic, weak) IBOutlet UIImageView *iconView;\n\n@property (nonatomic, strong) SACar *car;\n\n+ (id)carCell;\n\n+ (NSString *)ID;\n\n@end<\/pre>\n<p>3.4 SACarCell.m<\/p>\n<pre lang=\"objc\" line=\"1\">\/\/\n\/\/  SACarCell.m\n\/\/  XibCell\n\/\/\n\/\/  Created by yusian on 14-4-9.\n\/\/  Copyright (c) 2014\u5e74 yusian. All rights reserved.\n\/\/\n\n#import \"SACarCell.h\"\n#import \"SACAr.h\"\n\n@implementation SACarCell\n\n+ (id)carCell\n{\n    return [[NSBundle mainBundle] loadNibNamed:@\"Cell\" owner:nil options:nil][0];\n\/\/    UINib *nib = [UINib nibWithNibName:@\"Cell\" bundle:nil];\n\/\/    return [nib instantiateWithOwner:nil options:nil][0];\n}\n\n- (void)setCar:(SACar *)car\n{\n    _car = car;\n    _iconView.image = [UIImage imageNamed:car.icon];\n    _priceLable.text = car.price;\n    _sourceLable.text = [NSString stringWithFormat:@\"\u6765\u6e90:%@\", car.source];\n    _titleLable.text = car.title;\n}\n\n+ (NSString *)ID;\n{   \n    return @\"ID\";\n}\n\n@end<\/pre>\n<p>3.5 SATableViewController.h<\/p>\n<pre lang=\"objc\" line=\"1\">\/\/\n\/\/  SATableViewController.h\n\/\/  XibCell\n\/\/\n\/\/  Created by yusian on 14-4-4.\n\/\/  Copyright (c) 2014\u5e74 yusian. All rights reserved.\n\/\/\n\n#import &lt;UIKit\/UIKit.h&gt;\n#import \"SACar.h\"\n#import \"SACarCell.h\"\n\n@interface SATableViewController : UITableViewController\n\n@end<\/pre>\n<p>3.6 SATableViewController.m<\/p>\n<pre lang=\"objc\" line=\"1\">\/\/\n\/\/  SATableViewController.m\n\/\/  XibCell\n\/\/\n\/\/  Created by yusian on 14-4-4.\n\/\/  Copyright (c) 2014\u5e74 yusian. All rights reserved.\n\/\/\n\n#import \"SATableViewController.h\"\n\n@interface SATableViewController ()\n{\n    NSMutableArray *_cars;\n}\n\n@end\n\n@implementation SATableViewController\n\n#pragma mark - View\u52a0\u8f7d\u540e\u8c03\u7528\n- (void)viewDidLoad\n{\n    [super viewDidLoad];\n    \n    self.tableView.rowHeight = 80;  \/\/ \u5982\u679c\u6bcf\u884c\u56fa\u5b9a\u9ad8\u5ea6\u53ef\u7528\u8be5\u5c5e\u6027\u8bbe\u7f6ecell\u9ad8\u5ea6\n    \n    NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@\"Data.plist\" ofType:nil]];    \/\/ \u5c06plist\u6587\u4ef6\u6570\u636e\u52a0\u8f7d\u5230\u6570\u7ec4\n    \n    _cars = [NSMutableArray array]; \/\/ \u521d\u59cb\u5316\u7528\u6765\u5b58\u50a8\u6a21\u578b\u6570\u636e\u7684\u6570\u7ec4\n    \n    for (NSDictionary *dict in array) {\n        [_cars addObject:[SACar carWithDict:dict]];\n    }   \/\/ \u5c06\u6a21\u578b\u6570\u636e\u52a0\u8f7d\u5230\u6570\u7ec4\u5f53\u4e2d\n    \n}\n\n#pragma mark - Table view data source\n\n\/\/ \u8fd4\u56de\u8868\u683c\u884c\u6570\n- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section\n{\n    return [_cars count];   \/\/ \u6570\u7ec4\u957f\u5ea6\u5373\u4e3a\u8868\u683c\u884c\u6570\n}\n\n\/\/ \u8fd4\u56de\u8868\u683c\u5404\u884c\u4e2d\u5185\u5bb9\n- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath\n{\n    \/\/ \u56de\u6536\u6807\u8bb0\u7684cell\uff0c\u9075\u5faa\u5c01\u88c5\u7684\u539f\u5219\uff0c\u5c06ID\u5199\u8fdb\u89c6\u56fe\u7684\u7c7b\u65b9\u6cd5\u6765\u5b9e\u73b0\n    SACarCell *cell = [tableView dequeueReusableCellWithIdentifier:[SACarCell ID]];\n    \n    if (nil == cell) {\n        cell = [SACarCell carCell];     \/\/ \u8c03\u7528\u89c6\u56fe\u7684\u7c7b\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2acell\n    }\n    \n    cell.car = _cars[indexPath.row];    \/\/ \u5c06cell\u5bf9\u5e94\u7684\u6a21\u578b\u53d6\u51fa\u5c55\u793a\u5230\u89c6\u56fe\u4e2d\n    \n    return cell;\n}\n\n@end<\/pre>\n<p><strong>4\u3001\u6e90\u4ee3\u7801\u4e0b\u8f7d\uff1a<\/strong><a href=\"http:\/\/pan.baidu.com\/s\/1mgp0hpY\" target=\"_blank\">http:\/\/pan.baidu.com\/s\/1mgp0hpY<\/a> \u5bc6\u7801: tm29<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u6548\u679c\u5c55\u793a 2\u3001\u8bbe\u8ba1\u8bf4\u660e 2.0 \u51c6\u5907\u76f8\u5173\u5c55\u793a\u6570\u636e\uff0c\u7f16\u5199\u6210plist\u6587\u4ef6 2.1 \u5220\u9664\u4e4b\u524d\u7684\u63a7\u5236\u5668\uff0c\u65b0\u5efa\u4e00\u4e2a\u7c7bSATableViewController\uff0c\u7ee7\u627fUITableViewController\uff1b\uff08MVC\u4e2d\u7684Contrller\uff09 2.2 \u521b\u5efa\u4e00\u4e2axib\uff0c\u8bbe\u8ba1\u4e00\u4e2aUITableViewCell\u7684\u57fa\u672c\u6837\u5f0f\uff1b 2.3 \u521b\u5efa\u4e00\u4e2a\u7c7bSACarCell\uff0c\u8be5\u7c7b\u7528\u6765\u52a0\u8f7dxib\uff0c\u5e76\u7ed9xib\u4e2d\u5404\u63a7\u4ef6\u8d4b\u503c\uff1b\uff08MVC\u4e2d\u7684View\uff09 2.4 \u521b\u5efa\u4e00\u4e2a\u7c7bSACar\uff0c\u8be5\u7c7b\u7528\u6765\u521b\u5efa\u6570\u636e\u6a21\u578b\uff1b\uff08MVC\u4e2d\u7684Model\uff09 2.5 \u63a7\u5236\u5668\u64cd\u4f5c\uff1a2.5.1 \u901a\u8fc7\u6570\u7ec4\u52a0\u8f7dplist\u6587\u4ef6\u4e2d\u7684\u6570\u636e\uff0c\u518d\u7528\u6a21\u578b\u5c06\u6570\u636e\u683c\u5f0f\u5316\uff1b 2.5.2 \u5b9e\u73b0DataSource\u534f\u8bae\u7684\u4e24\u4e2a\u65b9\u6cd5\uff1a\u8868\u683c\u884c\u6570\uff0c\u8868\u683cCell\u5185\u5bb9 2.6 \u6a21\u578b\u64cd\u4f5c\uff1a 2.6.1 \u521b\u5efa\u6570\u636e\u7684\u5404\u4e2a\u5c5e\u6027\uff1b 2.6.2 \u5b9e\u73b0\u4e00\u4e2a\u7c7b\u65b9\u6cd5\uff0c\u5c06plist\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u8f6c\u6362\u6210\u6a21\u578b\u6570\u636e\uff1b 2.7 \u89c6\u56fe\u64cd\u4f5c\uff1a 2.7.1 \u7c7b\u65b9\u6cd5\u52a0\u8f7dxib\u6587\u4ef6\u4e2d\u7684\u57fa\u672c\u89c6\u56fe 2.7.2 xib\u4e2d\u7684\u5404\u4e2aview\u62bd\u8c61\u6210\u5c5e\u6027 2.7.3 \u5c06\u6a21\u578b\u62bd\u8c61\u6210\u89c6\u56fe\u7684\u4e00\u6761\u5c5e\u6027 2.7.4 \u91cd\u5199\u6a21\u578b\u5c5e\u6027\u7684set\u65b9\u6cd5\uff0c\u8ba9\u89c6\u56fe\u7ed3\u5408\u6a21\u578b\u5c06\u6570\u636e\u81ea\u52a8\u5c55\u793a\uff1b 3\u3001\u5173\u952e\u4ee3\u7801<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[17],"class_list":["post-563","post","type-post","status-publish","format-standard","hentry","category-project","tag-skill"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/563","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=563"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/563\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}