关键操作:
效果如下:
ViewController.h
1#import<UIKit/UIKit.h>23@interfaceViewController : UITableViewController4@PRoperty (strong, nonatomic) NSMutableArray *mArrDataList;5@property (strong, nonatomic) NSMutableArray *mArrImageList;67@end
ViewController.m
1#import"ViewController.h"2#import"KMTableViewCell.h"34@interfaceViewController ()5- (void)layoutUI;6- (void)loadData;7@end89@implementationViewController1011- (void)viewDidLoad {12[super viewDidLoad];1314[self layoutUI];15}1617- (void)didReceiveMemoryWarning {18[super didReceiveMemoryWarning];19//Dispose of any resources that can be recreated.20}212223- (void)layoutUI {24[self loadData];2526self.view.backgroundColor =[UIColor whiteColor];27self.navigationItem.title =@"自定义UITableViewCell";28}2930- (void)loadData {31NSBundle *bundle =[NSBundle mainBundle];32NSURL *urlFriendsInfo = [bundle URLForResource:@"FriendsInfo"withExtension:@"plist"];33NSDictionary *dicFriendsInfo =[NSDictionary dictionaryWithContentsOfURL:urlFriendsInfo];34NSInteger len =[dicFriendsInfo count];35_mArrDataList =[[NSMutableArray alloc] initWithCapacity:len];36_mArrImageList =[[NSMutableArray alloc] initWithCapacity:len];37for(NSInteger i=0; i<len; i++) {38NSString *strKey = [NSString stringWithFormat:@"%lu", (unsignedlong)(i+1)];39NSDictionary *dicData =[dicFriendsInfo objectForKey:strKey];40[_mArrDataList addObject:dicData];4142UIImage *img =[UIImage imageNamed:strKey];43[_mArrImageList addObject:img];44}45}4647#pragmamark - TableView DataSource and Delegate48- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {49return@"FriendsInfo列表";50}5152- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {53return1;54}5556- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {57return[_mArrDataList count];58}5960- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {61staticNSString *cellIdentifier =@"cellIdentifier";62staticBOOL isRegistered =NO;63if(!isRegistered) {64UINib *nib = [UINib nibWithNibName:@"KMTableViewCell"bundle:nil];65[tableView registerNib:nib forCellReuseIdentifier:cellIdentifier];66isRegistered =YES;67}68KMTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellIdentifier];69if(!cell) {70cell =[[KMTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];71}7273NSDictionary *rowData =_mArrDataList[indexPath.row];74cell.name = [rowData objectForKey:@"name"];75cell.desc = [rowData objectForKey:@"desc"];76cell.location = [rowData objectForKey:@"location"];77cell.imgCustom =_mArrImageList[indexPath.row];78returncell;79}8081- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {82return60.0;83}8485- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {86//从锚点位置出发,逆时针绕 Y 和 Z 坐标轴旋转90度87CATransform3D transform3D = CATransform3DMakeRotation(M_PI_2,0.0,1.0,1.0);8889//定义 cell 的初始状态90cell.alpha =0.0;91cell.layer.transform =transform3D;92cell.layer.anchorPoint = CGPointMake(0.0,0.5);//设置锚点位置;默认为中心点(0.5, 0.5)9394//定义 cell 的最终状态,执行动画效果95//方式一:普通操作设置动画96//[UIView beginAnimations:@"transform" context:NULL];97//[UIView setAnimationDuration:0.5];98//cell.alpha = 1.0;99//cell.layer.transform = CATransform3DIdentity;100//CGRect rect = cell.frame;101//rect.origin.x = 0.0;102//cell.frame = rect;103//[UIView commitAnimations];104105//方式二:代码块设置动画106[UIView animateWithDuration:0.5animations:^{107cell.alpha =1.0;108cell.layer.transform =CATransform3DIdentity;109CGRect rect =cell.frame;110rect.origin.x =0.0;111cell.frame =rect;112}];113}114115@end
KMTableViewCell.h
1#import<UIKit/UIKit.h>23@interfaceKMTableViewCell : UITableViewCell4@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;1213@end
KMTableViewCell.m
1#import"KMTableViewCell.h"23@implementationKMTableViewCell45- (void)awakeFromNib {6//Initialization code7}89- (void)setSelected:(BOOL)selected animated:(BOOL)animated {10[super setSelected:selected animated:animated];11//Configure the view for the selected state12}1314- (void)setImgCustom:(UIImage *)imgCustom {15if(![_imgCustom isEqual:imgCustom]) {16_imgCustom =[imgCustom copy];17_imgVCustom.image =_imgCustom;18}19}2021- (void)setName:(NSString *)name {22if(![_name isEqualToString:name]) {23_name =[name copy];24_lblName.text =_name;25}26}2728- (void)setDesc:(NSString *)desc {29if(![_desc isEqualToString:desc]) {30_desc =[desc copy];31_lblDesc.text =_desc;32}33}3435- (void)setLocation:(NSString *)location {36if(![_location isEqualToString:location]) {37_location =[location copy];38_lblLocation.text =_location;39}40}4142@end
KMTableViewCell.xib
1<?xmlversion="1.0" encoding="UTF-8" standalone="no"?>2<documenttype="com.apple.InterfaceBuilder3.CocoaTouch.XIB"version="3.0"toolsVersion="7531"systemVersion="14D136"targetRuntime="iOS.CocoaTouch"propertyaccessControl="none"useAutolayout="YES"useTraitCollections="YES">3<dependencies>4<plugInidentifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin"version="7520"/>5</dependencies>6<objects>7<placeholderplaceholderIdentifier="IBFilesOwner"id="-1"userLabel="File's Owner"/>8<placeholderplaceholderIdentifier="IBFirstResponder"id="-2"customClass="UIResponder"/>9<tableViewCellcontentMode="scaleToFill"selectionStyle="default"indentationWidth="10"reuseIdentifier="cellIdentifier"id="KGk-i7-Jjw"customClass="KMTableViewCell">10<rectkey="frame"x="0.0"y="0.0"width="320"height="60"/>11<autoresizingMaskkey="autoresizingMask"flexibleMaxX="YES"flexibleMaxY="YES"/>12<tableViewCellContentViewkey="contentView"opaque="NO"clipsSubviews="YES"multipleTouchEnabled="YES"contentMode="center"tableViewCell="KGk-i7-Jjw"id="H2p-sc-9uM">13<rectkey="frame"x="0.0"y="0.0"width="320"height="43"/>14<autoresizingMaskkey="autoresizingMask"/>15<subviews>16<imageViewuserInteractionEnabled="NO"contentMode="scaleToFill"horizontalHuggingPriority="251"verticalHuggingPriority="251"fixedFrame="YES"translatesAutoresizingMaskIntoConstraints="NO"id="3Br-R7-YsD">17<rectkey="frame"x="10"y="5"width="50"height="50"/>18</imageView>19<labelopaque="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<rectkey="frame"x="78"y="19"width="200"height="21"/>21<fontDescriptionkey="fontDescription"type="system"pointSize="12"/>22<colorkey="textColor"cocoaTouchSystemColor="darkTextColor"/>23<nilkey="highlightedColor"/>24</label>25<labelopaque="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<rectkey="frame"x="78"y="38"width="42"height="21"/>27<fontDescriptionkey="fontDescription"type="system"pointSize="12"/>28<colorkey="textColor"red="0.517578125"green="0.517578125"blue="0.517578125"alpha="1"colorSpace="calibratedRGB"/>29<nilkey="highlightedColor"/>30</label>31<labelopaque="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<rectkey="frame"x="78"y="0.0"width="42"height="21"/>33<fontDescriptionkey="fontDescription"type="boldSystem"pointSize="14"/>34<colorkey="textColor"cocoaTouchSystemColor="darkTextColor"/>35<nilkey="highlightedColor"/>36</label>37</subviews>38</tableViewCellContentView>39<connections>40<outletproperty="imgVCustom"destination="3Br-R7-YsD"id="ODd-v8-Lem"/>41<outletproperty="lblDesc"destination="dJA-8r-pcJ"id="SFw-6v-VAS"/>42<outletproperty="lblLocation"destination="5h7-UD-fzl"id="W60-wQ-S2r"/>43<outletproperty="lblName"destination="kPR-pa-8uG"id="BH7-oj-3Kx"/>44</connections>45</tableViewCell>46</objects>47</document>
AppDelegate.h
1#import<UIKit/UIKit.h>23@interfaceAppDelegate : UIResponder <UIapplicationDelegate>4@property (strong, nonatomic) UIWindow *window;5@property (strong, nonatomic) UINavigationController *navigationController;67@end
AppDelegate.m
1#import"AppDelegate.h"2#import"ViewController.h"34@interfaceAppDelegate ()5@end67@implementationAppDelegate89- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {10_window =[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];11ViewController *viewController =[[ViewController alloc] init];12_navigationController =[[UINavigationController alloc] initWithRootViewController:viewController];13_window.rootViewController =_navigationController;14//[_window addSubview:_navigationController.view];//当_window.rootViewController关联时,这一句可有可无15[_window makeKeyAndVisible];16returnYES;17}1819- (void)applicationWillResignActive:(UIApplication *)application {20}2122- (void)applicationDidEnterBackground:(UIApplication *)application {23}2425- (void)applicationWillEnterForeground:(UIApplication *)application {26}2728- (void)applicationDidBecomeActive:(UIApplication *)application {29}3031- (void)applicationWillTerminate:(UIApplication *)application {32}3334@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<plistversion="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>