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

关键操作:

效果如下:

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>
时间: 2024-10-25 06:59:53

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

Xcode中不用Storyboard,用纯xib创建TabBar模式视图

大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 如果要开发Tab类型视图的App,在Xcode中可以使用对应的模板 该模板自然使用的是Storyboard那一套东东,为了更清楚的了解xib布局,我们下面不用Storyboard模板来打造一个TabBar视图的App. 第一步:创建Single View App 打开Xcode,选择Single View App模板,创建新项目.然后将项目中的所有storyb

自定义单元格(IOS)

自定义单元格有三种方法 - 代码实现 - xib - storyboard(推荐) 在故事板中操作方法为 1.在TableView属性的Prototype Cells设置为1,默认为1: 2.需要创建自定义的单元格类: 3.设定Table View Cell的Class为自定义类: 自定义类:(并不难) #import "CustomCell.h" @implementation CustomCell - (void)awakeFromNib { // Initialization c

使用xib文件创建集合类单元格

UICollectionView是一种新的数据展示方式,简单来说可以把它理解成多列的UITableView.如果你用过iBooks的话,可能你还对书架布局有一定印象,一个虚拟书架上放着你下载和购买的各类图书,整齐排列.其实这就是一个UICollectionView的表现形式,或者iPad的iOS6中的原生时钟应用中的各个时钟,也是UICollectionView的最简单的一个布局. 集合视图UICollectionView介绍 集合视图UICollectionView和表视图UITableVie

Swift - 自定义单元格实现微信聊天界面

1,下面是一个放微信聊天界面的消息展示列表,实现的功能有: (1)消息可以是文本消息也可以是图片消息 (2)消息背景为气泡状图片,同时消息气泡可根据内容自适应大小 (3)每条消息旁边有头像,在左边表示发送方,在右边表示接收方 2,实现思路 (1)需要定义一个数据结构保存消息内容 MessageItem (2)继承UITableViewCell实现自定义单元格,这里面放入头像和消息体 (3)继承UITableView实现自定义表格,通过读取数据源,进行页面的渲染 (4)消息体根据内容类型不同,用不

FineReport单元格扩展与父子格设置

1.描述 在讲述报表设计之前,首先介绍一下FineReport报表制作的几个基本概念,本章节介绍FineReport报表赖以生存的单元格扩展. 扩展,顾名思义,就是由一变多,那么单元格扩展就是指在web端查看模板效果的时候,原来的单元格由一个变成了多个,这就是单元格扩展,如下图: 2. 单元格扩展 大家对Excel应该都不陌生,用过Excel的人都知道,其单元格只有2个方向,横向和纵向,而FineReport恰恰是一款类Excel的报表工具,其单元格也一样,因此,FineReport报表中单元格

用Xib创建控制器

注:用Xib创建控制器作为视图展示时,系统自己会自动去找之相似的xib文件,即(按照优先度从高到低)先找VSXibViewControll.xib文件,再找VSXibView.xib文件,再找与之相关多其他xib文件. 步骤一:xib文件名称与控制器类的名称无关. 1.创建一个VSXibViewControll.h, VSXibViewControll.m文件,再创建一个vsstar的xib文件. 2.点击VSstar.xib文件,给大视图加一个颜色,之后选择File`s Owner,选择右侧"

iOS UI-团购案例(通过xib文件自定义UITableViewCell)

一.Model 1 #import <Foundation/Foundation.h> 2 3 @interface Goods : NSObject 4 5 @property (nonatomic, copy) NSString *icon; 6 @property (nonatomic, copy) NSString *title; 7 @property (nonatomic, copy) NSString *price; 8 @property (nonatomic, copy) N

iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局

iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 一.项目文件结构和plist文件 二.实现效果 三.代码示例 1.没有使用配套的类,而是直接使用xib文件控件tag值操作 数据模型部分: YYtg.h文件 // // YYtg.h // 01-团购数据显示(没有配套的类) // // Created by apple on 14-5-29. // Copyright (c) 2014年 itcase. All rights reserved. //

【转】iOS 通过xib自定义UITableViewCell【原创】

原文网址:http://blog.it985.com/9683.html 在使用tableView的时候,如果cell的布局过于复杂,通过代码搭建的话不够直观.并且要不停的调整位置,字体什么的.这时,我们可以通过在tableViewCell的xib上搭建会更加直观,有效提高开发效率.首先,在我们创建了工程之后,新建XIB的cell.command+n,选择Cocoa Touch Class然后选择UITableViewCell类型,同时钩上Also Create xib File之后,在对应的c