码迷,mamicode.com
首页 > 其他好文 > 详细

114自定义 UITableViewCell(扩展知识:如何使用xib创建自定义的表格视图单元格 KMTableViewCell)

时间:2015-06-16 12:55:57      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

关键操作:

技术分享

技术分享

技术分享

效果如下:

技术分享

ViewController.h

1 #import <UIKit/UIKit.h>
2 
3 @interface ViewController : UITableViewController
4 @property (strong, nonatomic) NSMutableArray *mArrDataList;
5 @property (strong, nonatomic) NSMutableArray *mArrImageList;
6 
7 @end

ViewController.m

 1 #import "ViewController.h"
 2 #import "KMTableViewCell.h"
 3 
 4 @interface ViewController ()
 5 - (void)layoutUI;
 6 - (void)loadData;
 7 @end
 8 
 9 @implementation ViewController
10 
11 - (void)viewDidLoad {
12     [super viewDidLoad];
13     
14     [self layoutUI];
15 }
16 
17 - (void)didReceiveMemoryWarning {
18     [super didReceiveMemoryWarning];
19     // Dispose of any resources that can be recreated.
20 }
21 
22 - (void)layoutUI {
23     [self loadData];
24     
25     self.view.backgroundColor = [UIColor whiteColor];
26     self.navigationItem.title = @"自定义UITableViewCell";
27 }
28 
29 - (void)loadData {
30     NSBundle *bundle = [NSBundle mainBundle];
31     NSURL *urlFriendsInfo = [bundle URLForResource:@"FriendsInfo" withExtension:@"plist"];
32     NSDictionary *dicFriendsInfo = [NSDictionary dictionaryWithContentsOfURL:urlFriendsInfo];
33     NSInteger len = [dicFriendsInfo count];
34     _mArrDataList = [[NSMutableArray alloc] initWithCapacity:len];
35     _mArrImageList = [[NSMutableArray alloc] initWithCapacity:len];
36     for (NSInteger i=0; i<len; i++) {
37         NSString *strKey = [NSString stringWithFormat:@"%lu", (unsigned long)(i+1)];
38         NSDictionary *dicData = [dicFriendsInfo objectForKey:strKey];
39         [_mArrDataList addObject:dicData];
40         
41         UIImage *img = [UIImage imageNamed:strKey];
42         [_mArrImageList addObject:img];
43     }
44 }
45 
46 #pragma mark - TableView
47 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
48     return @"FriendsInfo列表";
49 }
50 
51 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
52     return 1;
53 }
54 
55 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
56     return [_mArrDataList count];
57 }
58 
59 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
60     static NSString *cellIdentifier = @"cellIdentifier";
61     static BOOL isRegistered = NO;
62     if (!isRegistered) {
63         UINib *nib = [UINib nibWithNibName:@"KMTableViewCell" bundle:nil];
64         [tableView registerNib:nib forCellReuseIdentifier:cellIdentifier];
65         isRegistered = YES;
66     }
67     KMTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
68     if (!cell) {
69         cell = [[KMTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
70     }
71     
72     NSDictionary *rowData = _mArrDataList[indexPath.row];
73     cell.name = [rowData objectForKey:@"name"];
74     cell.desc = [rowData objectForKey:@"desc"];
75     cell.location = [rowData objectForKey:@"location"];
76     cell.imgCustom = _mArrImageList[indexPath.row];
77     return cell;
78 }
79 
80 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
81     return 60.0;
82 }
83 
84 @end

KMTableViewCell.h

 1 #import <UIKit/UIKit.h>
 2 
 3 @interface KMTableViewCell : UITableViewCell
 4 @property (strong, nonatomic) IBOutlet UIImageView *imgVCustom;
 5 @property (strong, nonatomic) IBOutlet UILabel *lblName;
 6 @property (strong, nonatomic) IBOutlet UILabel *lblDesc;
 7 @property (strong, nonatomic) IBOutlet UILabel *lblLocation;
 8 @property (copy, nonatomic) UIImage *imgCustom;
 9 @property (copy, nonatomic) NSString *name;
10 @property (copy, nonatomic) NSString *desc;
11 @property (copy, nonatomic) NSString *location;
12 
13 @end

KMTableViewCell.m

 1 #import "KMTableViewCell.h"
 2 
 3 @implementation KMTableViewCell
 4 
 5 - (void)awakeFromNib {
 6     // Initialization code
 7 }
 8 
 9 - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
10     [super setSelected:selected animated:animated];
11     // Configure the view for the selected state
12 }
13 
14 - (void)setImgCustom:(UIImage *)imgCustom {
15     if (![_imgCustom isEqual:imgCustom]) {
16         _imgCustom = [imgCustom copy];
17         _imgVCustom.image = _imgCustom;
18     }
19 }
20 
21 - (void)setName:(NSString *)name {
22     if (![_name isEqualToString:name]) {
23         _name = [name copy];
24         _lblName.text = _name;
25     }
26 }
27 
28 - (void)setDesc:(NSString *)desc {
29     if (![_desc isEqualToString:desc]) {
30         _desc = [desc copy];
31         _lblDesc.text = _desc;
32     }
33 }
34 
35 - (void)setLocation:(NSString *)location {
36     if (![_location isEqualToString:location]) {
37         _location = [location copy];
38         _lblLocation.text = _location;
39     }
40 }
41 
42 @end

KMTableViewCell.xib

 1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
 2 <document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="7531" systemVersion="14D136" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
 3     <dependencies>
 4         <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7520"/>
 5     </dependencies>
 6     <objects>
 7         <placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File‘s Owner"/>
 8         <placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
 9         <tableViewCell contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="cellIdentifier" id="KGk-i7-Jjw" customClass="KMTableViewCell">
10             <rect key="frame" x="0.0" y="0.0" width="320" height="60"/>
11             <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
12             <tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="KGk-i7-Jjw" id="H2p-sc-9uM">
13                 <rect key="frame" x="0.0" y="0.0" width="320" height="43"/>
14                 <autoresizingMask key="autoresizingMask"/>
15                 <subviews>
16                     <imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="3Br-R7-YsD">
17                         <rect key="frame" x="10" y="5" width="50" height="50"/>
18                     </imageView>
19                     <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Label" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="dJA-8r-pcJ">
20                         <rect key="frame" x="78" y="19" width="200" height="21"/>
21                         <fontDescription key="fontDescription" type="system" pointSize="12"/>
22                         <color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
23                         <nil key="highlightedColor"/>
24                     </label>
25                     <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Label" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="5h7-UD-fzl">
26                         <rect key="frame" x="78" y="38" width="42" height="21"/>
27                         <fontDescription key="fontDescription" type="system" pointSize="12"/>
28                         <color key="textColor" red="0.517578125" green="0.517578125" blue="0.517578125" alpha="1" colorSpace="calibratedRGB"/>
29                         <nil key="highlightedColor"/>
30                     </label>
31                     <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Label" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="kPR-pa-8uG">
32                         <rect key="frame" x="78" y="0.0" width="42" height="21"/>
33                         <fontDescription key="fontDescription" type="boldSystem" pointSize="14"/>
34                         <color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
35                         <nil key="highlightedColor"/>
36                     </label>
37                 </subviews>
38             </tableViewCellContentView>
39             <connections>
40                 <outlet property="imgVCustom" destination="3Br-R7-YsD" id="ODd-v8-Lem"/>
41                 <outlet property="lblDesc" destination="dJA-8r-pcJ" id="SFw-6v-VAS"/>
42                 <outlet property="lblLocation" destination="5h7-UD-fzl" id="W60-wQ-S2r"/>
43                 <outlet property="lblName" destination="kPR-pa-8uG" id="BH7-oj-3Kx"/>
44             </connections>
45         </tableViewCell>
46     </objects>
47 </document>

AppDelegate.h

1 #import <UIKit/UIKit.h>
2 
3 @interface AppDelegate : UIResponder <UIApplicationDelegate>
4 @property (strong, nonatomic) UIWindow *window;
5 @property (strong, nonatomic) UINavigationController *navigationController;
6 
7 @end

AppDelegate.m

 1 #import "AppDelegate.h"
 2 #import "ViewController.h"
 3 
 4 @interface AppDelegate ()
 5 @end
 6 
 7 @implementation AppDelegate
 8 
 9 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
10     _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
11     ViewController *viewController = [[ViewController alloc] init];
12     _navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
13     _window.rootViewController = _navigationController;
14     //[_window addSubview:_navigationController.view]; //当_window.rootViewController关联时,这一句可有可无
15     [_window makeKeyAndVisible];
16     return YES;
17 }
18 
19 - (void)applicationWillResignActive:(UIApplication *)application {
20 }
21 
22 - (void)applicationDidEnterBackground:(UIApplication *)application {
23 }
24 
25 - (void)applicationWillEnterForeground:(UIApplication *)application {
26 }
27 
28 - (void)applicationDidBecomeActive:(UIApplication *)application {
29 }
30 
31 - (void)applicationWillTerminate:(UIApplication *)application {
32 }
33 
34 @end

FriendsInfo.plist

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  3 <plist version="1.0">
  4 <dict>
  5     <key>1</key>
  6     <dict>
  7         <key>name</key>
  8         <string>小明</string>
  9         <key>desc</key>
 10         <string>干啥呢?</string>
 11         <key>location</key>
 12         <string>广州</string>
 13     </dict>
 14     <key>2</key>
 15     <dict>
 16         <key>name</key>
 17         <string>痞子</string>
 18         <key>desc</key>
 19         <string>好好学习,天天向上!</string>
 20         <key>location</key>
 21         <string>广州</string>
 22     </dict>
 23     <key>3</key>
 24     <dict>
 25         <key>name</key>
 26         <string>疯子</string>
 27         <key>desc</key>
 28         <string>倚楼听风雨,淡看江湖路。</string>
 29         <key>location</key>
 30         <string>广州</string>
 31     </dict>
 32     <key>4</key>
 33     <dict>
 34         <key>name</key>
 35         <string>梦醒</string>
 36         <key>desc</key>
 37         <string>书到用时方恨少</string>
 38         <key>location</key>
 39         <string>广州</string>
 40     </dict>
 41     <key>5</key>
 42     <dict>
 43         <key>name</key>
 44         <string>落落</string>
 45         <key>desc</key>
 46         <string>生日快乐!</string>
 47         <key>location</key>
 48         <string>广州</string>
 49     </dict>
 50     <key>6</key>
 51     <dict>
 52         <key>name</key>
 53         <string>丫丫</string>
 54         <key>desc</key>
 55         <string>做个踏实的科研女</string>
 56         <key>location</key>
 57         <string>广州</string>
 58     </dict>
 59     <key>7</key>
 60     <dict>
 61         <key>name</key>
 62         <string>乐天平</string>
 63         <key>desc</key>
 64         <string>在火车上</string>
 65         <key>location</key>
 66         <string>广州</string>
 67     </dict>
 68     <key>8</key>
 69     <dict>
 70         <key>name</key>
 71         <string>北暮</string>
 72         <key>desc</key>
 73         <string>好久不见!</string>
 74         <key>location</key>
 75         <string>广州</string>
 76     </dict>
 77     <key>9</key>
 78     <dict>
 79         <key>name</key>
 80         <string>苹果</string>
 81         <key>desc</key>
 82         <string>喜欢苹果,更喜欢青苹果!</string>
 83         <key>location</key>
 84         <string>广州</string>
 85     </dict>
 86     <key>10</key>
 87     <dict>
 88         <key>name</key>
 89         <string>木头</string>
 90         <key>desc</key>
 91         <string>清心薄欲 静躁作学</string>
 92         <key>location</key>
 93         <string>广州</string>
 94     </dict>
 95     <key>11</key>
 96     <dict>
 97         <key>name</key>
 98         <string>醉清风</string>
 99         <key>desc</key>
100         <string>一醉解千愁</string>
101         <key>location</key>
102         <string>广州</string>
103     </dict>
104     <key>12</key>
105     <dict>
106         <key>name</key>
107         <string>浅の斯</string>
108         <key>desc</key>
109         <string>想剪短发……剪还是不剪(⊙o⊙)?</string>
110         <key>location</key>
111         <string>广州</string>
112     </dict>
113     <key>13</key>
114     <dict>
115         <key>name</key>
116         <string>虚伪</string>
117         <key>desc</key>
118         <string>讨厌虚伪</string>
119         <key>location</key>
120         <string>广州</string>
121     </dict>
122     <key>14</key>
123     <dict>
124         <key>name</key>
125         <string>阁楼</string>
126         <key>desc</key>
127         <string>窗外的风景。</string>
128         <key>location</key>
129         <string>广州</string>
130     </dict>
131 </dict>
132 </plist>

 

114自定义 UITableViewCell(扩展知识:如何使用xib创建自定义的表格视图单元格 KMTableViewCell)

标签:

原文地址:http://www.cnblogs.com/huangjianwu/p/4580317.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!