首页 > 其他分享 >iOS开发UITableView基本使用方法总…

iOS开发UITableView基本使用方法总…

时间:2023-04-30 11:03:24浏览次数:34  
标签:indexPath NSInteger NSIndexPath tableView section iOS 开发 UITableView


UITableView基本使用方法

1.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDataSource

2.然后 UITableView对象的 delegate要设置为 self。

3.然后就可以实现这些delegate的一些方法拉。

(1)- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

这个方法返回 tableview 有多少个section



1. 
2. (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
3.  
4.  return1;  
5.

(2)- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section;

 对应的section有多少个元素,也就是多少行。



1. (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
2.  
3.  return10;  
4.

(3)- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath;

这个方法返回指定的 row 的高度。

:(NSInteger)section;

这个方法返回指定的 section的header view 的高度。

:(NSInteger)section;

这个方法返回指定的 section的footer view 的高度。



1. (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
2.  
3.  staticNSString * showUserInfoCellIdentifier = @"ShowUserInfoCell";  
4. * cell = [tableView_ dequeueReusableCellWithIdentifier:showUserInfoCellIdentifier];  
5.  if(cell == nil)  
6.  
7. Create a cell to display an ingredient. 
8. = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle  
9. er]  
10.  
11.  
12. 
13. Configure the cell. 
14. "签名";  
15. = [NSString stringWithCString:userInfo.user_signature.c_str() encoding:NSUTF8StringEncoding];  
16.

(5)- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

返回指定的 section 的header的高度





1. (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section  
2.  
3.  if(section ==0)  
4.  return80.0f;  
5.  else 
6.  return30.0f;  
7.

(6)- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

 的 title,如果这个section header  有返回view,那么title就不起作用了。



1. (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section  
2.  
3.  if(tableView == tableView_)  
4.  
5.  if(section == 0)  
6.  
7.  return@"title 1";  
8.  
9.  elseif(section == 1)  
10.  
11.  return@"title 2";  
12.  
13.  else 
14.  
15.  returnnil;  
16.  
17.  
18.  else 
19.  
20.  returnnil;  
21.  
22.

(7) - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

返回指定的 section header 的view,如果没有,这个函数可以不返回view



1. (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section  
2.  
3.  if(section == 0)  
4.  
5. 
6. header = [[[NSBundle mainBundle] loadNibNamed: @"SettingHeaderView" 
7. self  
8. nil] lastObject];  
9. 
10.  else 
11.  
12.  returnnil;  
13.  
14.

(8)  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

当用户选中某个行的cell的时候,回调用这个。但是首先,必须设置tableview的一个属性为可以select 才行。







  1. StyleBlue;  

如果不希望响应select,那么就可以用下面的代码设置属性:




  1.  

下面是响应select 点击函数,根据哪个section,哪个row 自己做出响应就好啦。




1. (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
2.  
3.  if(indexPath.section == 1)  
4.  
5.  return;  
6.  
7.  elseif(indexPath.section==0)  
8.  
9.  switch(indexPath.row)  
10.  
11. 
12.  case0:  
13.  
14. onTalkToFriendBtn];  
15.  
16.  break;  
17. 
18.  default:  
19.  break;  
20.  
21.  
22.  else 
23.  
24.  return;  
25.  
26. 
27.

如何让cell 能够响应 select,但是选中后的颜色又不发生改变呢,那么就设置

StyleNone;



1. (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
2.  
3. 
4. = UITableViewCellSelectionStyleNone;  
5.

(9)如何设置tableview  每行之间的分割线




  1. StyleSingleLine;  

StyleNone  即可。

(10)如何设置 tableview cell的背景颜色



1. (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
2.  
3. 
4. colorWithRed:0.957 green:0.957 blue:0.957 alpha:1];  
5.

(11) - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath

这个函数响应,用户点击cell 右边的 箭头(如果有的话)

(12)如何设置tableview 可以被编辑

首先要进入编辑模式:



1. setEditing:YES animated:YES];

如果要退出编辑模式,肯定就是设置为NO

yle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

 要执行的是哪种编辑,下面的代码是返回删除模式





1. (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath  
2.  
3.  returnUITableViewCellEditingStyleDelete;  
4.

-(void) tableView:(UITableView *)aTableView

yle) editingStyle

forRowAtIndexPath:(NSIndexPath *)indexPath

通知告诉用户编辑了 哪个cell,对应上面的代码,我们在这个函数里面执行删除cell的操作。



1.  -(void) tableView:(UITableView *)aTableView  
2. yle) editingStyle  
3. *)indexPath  
4.  
5. removeObjectAtIndex:indexPath.row];  
6. reloadData];  
7.

(13)如何获得 某一行的CELL对象

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;

标签:indexPath,NSInteger,NSIndexPath,tableView,section,iOS,开发,UITableView
From: https://blog.51cto.com/u_548275/6237795

相关文章

  • Android 开发 UI 规则
    Android的官方开发者博客发了一份幻灯片,介绍了一些AndroidUI设计的小贴士,在这里以看图说话的形式发出来。Don’t: 1、不要照搬你在其它平台的UI设计,应该让用户感觉是在真正使用一个Android软件,在你的商标显示和平台整体观感之间做好平衡2、不要过度使用模态对话框3、......
  • Android开发中立即停止AsyncTask和…
    程序开发中经常会有立即停止线程运行的需要,而通过api是没有办法实现这个目的的,因此对于这个情况,我们需要采取一些“特殊”的方式来实现:  1,判断标志位的办法:  我们要知道在java的线程中,没有办法停止一个正在运行中的线程。在Android的AsyncTask中也是一样的。如果必须要停......
  • Vue 异步通信Axios
    使用Axios实现异步通信需要先导入cdn:<scriptsrc="https://unpkg.com/[email protected]/dist/axios.min.js"></script>使用到的数据data.json{"name":"kuang","url":"https://www.bilibili.com/?spm_id_from=333.788.0......
  • SpringMVC使用注解开发
    1.编写web.xml(模板代码)ViewCode2.导入springmvc的context和mvc两个依赖,通过context标签可以自动扫描识别包"com.lian.controller"下的所有注解,两个mvc标签是默认配置;context和mvc分别替代了之前的处理器映射器HandleMapper和处理器适配器HandlerAdapter;视图解析器拼接要要跳转......
  • dell 7080m black mac bios setup
    BISO设置参考的以下帖子,改了一部分内容USBWakeSupport和WakeonLAN/WLAN保持了默认,因为我用不到网络唤醒功能。​https://github.com/3dudu/dell-optiplex-7080-hackintosh-opencore设置项   值SATAOperation   AHCIIntegratedNIC   EnabledSecureBootEnable ......
  • 驱动开发:通过MDL映射实现多次通信
    在前几篇文章中LyShark通过多种方式实现了驱动程序与应用层之间的通信,这其中就包括了通过运用SystemBuf缓冲区通信,运用ReadFile读写通信,运用PIPE管道通信,以及运用ASYNC反向通信,这些通信方式在应对一收一发模式的时候效率极高,但往往我们需要实现一次性吐出多种数据,例如ARK工具中当我......
  • 驱动开发:通过MDL映射实现多次通信
    在前几篇文章中LyShark通过多种方式实现了驱动程序与应用层之间的通信,这其中就包括了通过运用SystemBuf缓冲区通信,运用ReadFile读写通信,运用PIPE管道通信,以及运用ASYNC反向通信,这些通信方式在应对一收一发模式的时候效率极高,但往往我们需要实现一次性吐出多种数据,例如ARK工具中当......
  • C#WinForm开发坦克大战
    1usingSystem.Drawing;2usingSystem.Threading;3usingSystem.Windows.Forms;45///<summary>6///窗体7///</summary>8namespace_03_WinForm9{10publicpartialclassForm1:Form11{12//子线程13pri......
  • Linux设备驱动开发详解
    Linux内核系列文章Linux内核设计与实现深入理解Linux内核Linux设备驱动程序Linux设备驱动开发详解文章目录Linux内核系列文章前言一、待续前言  本文主要用来摘录《Linux设备驱动开发详解第四版》一书中学习知识点,本书基于Linux4.0版本,源代码摘录基于Linux4.15.18......
  • (2023)iOS17开放侧载的网友观点调研
    前言因为欧盟方面的强制措施,不出意外的话,iOS17开始苹果将被迫开放侧载。虽然具体如何开放的细节还不确定,但是这毕竟对苹果,开发者,以及用户都是不小的事情。整理了下网友们(主要是开发者们),对侧载的一系列看法和猜测。因为很多意见是相左的,所以整理成了反面观点和正面观点。反面......