iOS UITableViewCell UITableVIewController 纯代码开发
iOS UITableViewCell UITableVIewController 纯代码开发 <原创> 1.纯代码 自定义UITableViewCell 直接上代码 ////// #import <UIKit/UIKit.h> @interface CodeTableViewCell : UITableViewCell @property (nonatomic, weak) UIImageView *iconView; @property (nonatomic, weak) UILabel *labName; + (instancetype)cellWithTableView:(UITableView *)tableView; @end /// #import "CodeTableViewCell.h" #define NJNameFont [UIFont systemFontOfSize:15] #define NJTextFont [UIFont systemFontOfSize:16] @implementation CodeTableViewCell + (instancetype)cellWithTableView:(UITableView *)tableView { // NSLog(@"cellForRowAtIndexPath"); static NSString *identifier = @"status"; // 1.缓存中取 CodeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; // 2.创建 if (cell == nil) { cell = [[CodeTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; } return cell; } /** * 构造方法(在初始化对象的时候会调用) * 一般在这个方法中添加需要显示的子控件 */ - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // 让自定义Cell和系统的cell一样, 一创建出来就拥有一些子控件提供给我们使用 // 1.创建头像 UIImageView *iconView = [[UIImageView alloc] init]; [self.contentView addSubview:iconView]; self.iconView = iconView; // 2.创建昵称 UILabel *nameLabel = [[UILabel alloc] init]; nameLabel.font = NJNameFont; nameLabel.textColor=[UIColor redColor]; [self.contentView addSubview:nameLabel]; self.labName = nameLabel; [self.contentView setBackgroundColor:[UIColor clearColor]]; [self settingFrame]; } return self; } //设置相对位置 - (void)settingFrame { self.frame = CGRectMake(0, 0, 320, 120); self.labName.frame=CGRectMake(100, 120/2-25, 200, 50); self.iconView.frame=CGRectMake(10, (120-80)/2, 80, 80); } - (void)awakeFromNib { // Initialization code } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; // Configure the view for the selected state } @end ////// 2. 通常写UITableView 都是 在UIViewController里面定义一个UITableView,现在直接让当前控制器继承于UITableViewController,避免写繁文缛节的死代码,更方便.但是table view controller 只限于管理一个全屏展示的 table view。 最后,你需要把迁移后丢失的 table view controller 的特性给补回来。大多数都是 viewWillAppear: 或 viewDidAppear: 中简单的一条语句。 直接上代码: #import <UIKit/UIKit.h> @interface RootViewController : UITableViewController @end ///// #import "RootViewController.h" #import "CodeTableViewCell.h" @interface RootViewController ()<UITableViewDataSource,UITableViewDelegate> { UITableView *tableViewMine; } @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor=[UIColor yellowColor]; // tableViewMine=[[UITableView alloc]initWithFrame:self.view.frame]; // [self.view addSubview:tableViewMine]; // tableViewMine.delegate=self; // tableViewMine.dataSource=self; } -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 120; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 6; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPat {//调用 自定义的tableViewCell CodeTableViewCell *cell=[CodeTableViewCell cellWithTableView:tableView]; cell.labName.text=@"sssssssssss"; cell.iconView.image=[UIImage imageNamed:@"iconhead.jpg"]; return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"%ld",indexPath.row); } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end ///////////
上效果图:
posted on 2015-01-26 14:39 ACM_Someone like you 阅读(1196) 评论(0) 编辑 收藏 举报
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)