依据不同须要,须要使用tableview的结构,可是里面每个cell,又须要自己的样式。所以学习了一下如何把自定义的cell加到tableview里面

首先要自己创建一个类,继承UITableViewCell,然后新建一个空的xib文件,并在class属性设置为相应的类名

代码部分:

  1. <pre name="code" class="objc">
  2. #import "SettingViewController.h"
  3. #import "SettingViewCell.h"
  4. @interface SettingViewController ()
  5. @end
  6. @implementation SettingViewController
  7. @synthesize listArray =_listArray;
  8. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  9. {
  10. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  11. if (self) {
  12. // Custom initialization
  13. }
  14. return self;
  15. }
  16. - (void)viewDidLoad
  17. {
  18. [super viewDidLoad];
  19. // Do any additional setup after loading the view from its nib.
  20. NSArray *array = [NSArray arrayWithObjects: @"房间设置",@"灯光设置",@"窗帘设置",@"场景设置",@"空调设置",@"安防设置",@"网络设置",nil];
  21. _settingview.dataSource =self; //设置数据源方法
  22. _settingview.delegate =self;//设置代理方法
  23. self.listArray =array; //注意。因为内存管理问题。假设不正确self.listArray进行赋值,则_listArray会被清空。程序会崩溃
  24. }
  25. - (IBAction)backClicked:(id)sender {
  26. [self.navigationController popToRootViewControllerAnimated:YES];
  27. }
  28. //数据源方法
  29. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  30. {
  31. return [_listArray count];
  32. }
  33. //托付方法
  34. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  35. {
  36. switch(indexPath.row)
  37. {
  38. case 0:
  39. break;
  40. default:
  41. break;
  42. }
  43. }
  44. // Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
  45. // Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
  46. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  47. {
  48. static NSString * cellIdentifier =@"mycell";
  49. SettingViewCell *cell =(SettingViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  50. if(cell == nil)
  51. {
  52. NSArray *array =[[NSBundle mainBundle] loadNibNamed:@"SettingViewCell" owner:self options:nil];
  53. cell = [array objectAtIndex: 0];
  54. [cell setSelectionStyle:UITableViewCellSelectionStyleGray];
  55. }
  56. NSString *text = _listArray[indexPath.row];
  57. [[cell settingName] setText:text];
  58. return cell;
  59. }
  60. - (void)didReceiveMemoryWarning
  61. {
  62. [super didReceiveMemoryWarning];
  63. // Dispose of any resources that can be recreated.
  64. }
  65. - (void)dealloc {
  66. [_back release];
  67. [_settingview release];
  68. [super dealloc];
  69. }
  70. @end
  1.  

IOS 自己定义UITableView的更多相关文章

  1. iOS开发系列--UITableView全面解析

    --UIKit之UITableView 概述 在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似于微信.QQ.新浪微博等软件基本上随处都是U ...

  2. IOS开发中UITableView(表视图)的滚动优化及自定义Cell

    IOS开发中UITableView(表视图)的滚动优化及自定义Cell IOS 开发中UITableView是非常常用的一个控件,我们平时在手机上看到的联系人列表,微信好友列表等都是通过UITable ...

  3. 【iOS系列】- UITableView的使用技巧

    [iOS系列]- UITableView的使用 UITableView的常用属性 indexpath.row:行 indexpath.section:组 separatorColor:分割线的颜色 s ...

  4. ios宏定义字符串

    ios宏定义字符串 #define objcString(str) @""#str"" 使用效果: objcString(字符串)

  5. iOS开发之UITableView的使用

    这一篇记录的是iOS开发中UITableView的使用,iOS中的UITableView跟Android中的ListView特别相似,以下用一个Demo来说明: 1.Xcode中新建projectTe ...

  6. iOS Programming Editing UITableView

    iOS Programming Editing UITableView 1.1 Editing mode  UITableView has an editing property, and when ...

  7. Ios 该图显示其出现的相关问题定义UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:&#39;

    解决这个问题 在 加上个 标示符 Cell 自己定义 customCell .h 代码例如以下 ViewController.m 文件里 代码例如以下 执行结果 吕 图坚持直接在这里 不行

  8. iOS开发之UITableView使用总结

    什么是UITableView 在众多移动应用中,能看到各式各样的表格数据 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView UITableView继承自UIScrollVie ...

  9. iOS 8自动调整UITableView和UICollectionView布局

    本文转载自:http://tech.techweb.com.cn/thread-635784-1-1.html 本文讲述了UITableView.UICollectionView实现 self-siz ...

随机推荐

  1. hbase 单机+伪分布环境搭建学习-1

    1.单机模式: (1)编辑hbase-env.sh user@EBJ1023.local:/usr/local/flume_kafka_stom/hbase_1.1.2> vim conf/hb ...

  2. [hibernate]org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter

    org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type sette ...

  3. NHibernate之映射文件配置说

    1. hibernate-mapping 这个元素包括以下可选的属性.schema属性,指明了这个映射所引用的表所在的schema名称.假若指定了这个属性, 表名会加上所指定的schema的名字扩展为 ...

  4. Spring------SpringBoot参考书籍

    转载: http://download.csdn.net/download/plus_dy/8972653

  5. 1-0 superset的安装和配置

    Superset安装及教程官网(http://airbnb.io/superset/installation.html)讲解的已经够详细的了,本篇以官网教程为蓝本进行说明. 入门 Superset目前 ...

  6. shell基础篇(四)算术运算

    ---内容来源于http://www.jb51.net/article/31232.htm shell中的赋值和操作默认都是字符串处理,1.错误方法举例 a) var=1+1 echo $var 输出 ...

  7. java中Double的isInfinite()和isNaN()

    在Double和Float类中都有这两个方法,用于判断是否是无穷大及是否为非数字 public boolean isInfinite()如果此对象表示的值是正无穷大或负无穷大,则返回 true:否则返 ...

  8. RF使用的相关库API

    RF内置库: http://robotframework.org/robotframework/ SSHLibrary:   ---WEB自动化测试 http://robotframework.org ...

  9. 使用Editplus和Dev C++配置C++的编译运行环 境

    或许大家会有疑问,为何不直接使用VC;VS;或Dev这些IDE呢?何必舍近求远.主要是因为写程序这么多年来已经习惯了Editplus,包括他的快捷键,语法自动完成,语法提示等等,Editplus用了这 ...

  10. js里面正则表示满足多个条件的写法

    实例,满足条件里面必须包含数字,字母和下划线组成 代码如下: var reg = /^([a-z]+\d+\_+)|([a-z]+\_+\d+)|(\_+[a-z]+\d+)|(\_+\d+[a-z] ...