UITableView的一些基本用法

本文详细介绍了UITableView的基本使用方法,包括设置代理、数据源、单元格复用机制、单元格样式定制等,并提供了设置表格视图高度、标题、背景色及响应用户交互的方法。

UITableView继承自UIScrollView

第一步: 在你的Controller的后面加上<UITableViewDelegate, UITableViewDataSource>

第二步: 将UITableView的delegate设置为self

第三步: 实现这些delegate的方法,

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

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

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
      return 1;     //number of the sections
}



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

这个方法将返回每个section有多少元素, 一般我们声明一个数组, 把将要显示的内容放置到数组里面, 这里的section的数量一般要和array.count一致.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section   
{  
    return array.count;   //number of rows in section
}  


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

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

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

返回指定section的header view 的高度

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;

返回指定section的footer view 的高度


4. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath;

返回指定row 的cell。这个地方是比较关键的地方,一般在这个地方来定制各种个性化的 cell元素。

这里只是使用最简单最基本的cell 类型。

cell.textLabel :主标题

cell.detailTextLabel: 副标题

cell.imageView: 每个cell最前面还可以插入image  

cell.accessoryType: 设置右边的图标,通过可以设置是饱满的向右的蓝色箭头,还是单薄的向右箭头,还是对勾标记.

每一个UITableView里都维护着一个cell队列,当UITableView刚加载的时候,cell队列里是没有任何数据的。dequeueResableCellWithIdentifier从字面上理解就是”出列可重用的cell",也就是根据一个标识identifier从cell队列里取出一个UITableViewCell,当然了,如果cell队列里没有此标识的cell,调用此方法的结果就是返回nil。因此,在UITableView刚加载的时候,cell队列里没有可用的cell,所以必须通过语句下面的if循环中的语句来创建一个新的cell.

当UITableView在滚动的时候导致UITableViewCell滚出手机屏幕视图的时候,程序会将这一个UITalbeViewCell实例放入此UITableView所维护的cell队列中。当UITableview中有新的UITableViewCell需要展现在手机屏幕视图上时,就会调用tableView:cellForRowAtIndexPath:方法了,因此我们可以知道以下几点:
1-重取出来的cell是有可能已经捆绑过数据或者加过子视图的,所以,如果有必要,要清除数据(比如textlabel的text)和remove掉add过的子视图(使用tag)
2-这样设计的目的是为了避免频繁的 alloc和delloc cell对象而已,没有多复杂
3-设计的关键是实现cell和数据的完全分离

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath   
{  
    static NSString * showUserInfoCellIdentifier = @"ShowUserInfoCell";  
    UITableViewCell * cell = [_tableView dequeueReusableCellWithIdentifier:showUserInfoCellIdentifier];
    if (cell == nil)  
    {  
        // Create a cell to display an ingredient.
	//创建对应CellIdentifier标识的UITableViewCell实例  
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle   
                                       reuseIdentifier:showUserInfoCellIdentifier]   
                autorelease];  
    }  
      
    // Configure the cell.  
    cell.textLabel.text=@"签名";  
    cell.detailTextLabel.text = [NSString stringWithCString:userInfo.user_signature.c_str()  encoding:NSUTF8StringEncoding];  
        }  




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

返回指定的section的header的高度

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section  
{  
    if (section ==0)  
        return 80.0f;  
    else  
        return 30.0f;  
}  


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

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

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section  
{  
    if (tableView == tableView_)  
    {  
        if (section == 0)   
        {  
            return @"title 1";  
        }   
        else if (section == 1)   
        {  
            return @"title 2";  
        }   
        else   
        {  
            return nil;  
        }  
    }   
    else   
    {  
        return nil;  
    }  
}  


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

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

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section  
{  
    if (section == 0)   
    {  
          
        UIView* header = [[[NSBundle mainBundle] loadNibNamed: @"SettingHeaderView"   
                                                        owner: self  
                                                      options: nil] lastObject];  
       
        else  
        {  
           return nil;  
        }  
}  


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

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

TableView.allowsSelection=YES;  
cell.selectionStyle=UITableViewCellSelectionStyleBlue; 
如果不希望相应select, 那么就可以用下面的代码设置属性

TableView.allowsSelection=NO;  
 下面是响应select 点击函数,根据哪个section,哪个row 自己做出响应就好了

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath   
{  
    if (indexPath.section == 1)   
    {  
        return;  
    }  
    else if(indexPath.section==0)  
    {  
        switch (indexPath.row)   
        {  
            //聊天  
            case 0:  
            {  
                [self  onTalkToFriendBtn];  
            }  
                break;  
                  
            default:  
                break;  
        }  
    }  
    else   
    {  
        return ;  
    }  
      
}  

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

cell.selectionStyle = UITableViewCellSelectionStyleNone;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
{  
    //cell被选中后的颜色不变  
    cell.selectionStyle = UITableViewCellSelectionStyleNone;  
}  

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

self.tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
如果不需要分割线,那么就设置属性为 UITableViewCellSeparatorStyleNone  即可。


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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
{  
        //设置背景颜色  
        cell.contentView.backgroundColor=[UIColor colorWithRed:0.957 green:0.957 blue:0.957 alpha:1];  
}  

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

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


12.如何设置tableview 可以被编辑

               首先要进入编辑模式:

[TableView setEditing:YES animated:YES]; 
如果要退出编辑模式,肯定就是设置为NO

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
返回当前cell  要执行的是哪种编辑,下面的代码是 返回 删除  模式

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath  
{  
    return UITableViewCellEditingStyleDelete;  
}  

  -(void) tableView:(UITableView *)aTableViewcommitEditingStyle:(UITableViewCellEditingStyle) editingStyleforRowAtIndexPath:(NSIndexPath *)indexPath;

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

-(void) tableView:(UITableView *)aTableView  
commitEditingStyle:(UITableViewCellEditingStyle) editingStyle  
forRowAtIndexPath:(NSIndexPath *)indexPath  
{  
        [chatArray  removeObjectAtIndex:indexPath.row];  
    [chatTableView  reloadData];  
}  

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

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



下载前可以先看下教程 https://pan.quark.cn/s/a426667488ae 标题“仿淘宝jquery图片左右切换带数字”揭示了这是一个关于运用jQuery技术完成的图片轮播机制,其特色在于具备淘宝在线平台普遍存在的图片切换表现,并且在整个切换环节中会展示当前图片的序列号。 此类功能一般应用于电子商务平台的产品呈现环节,使用户可以便捷地查看多张商品的照片。 说明中的“NULL”表示未提供进一步的信息,但我们可以借助标题来揣摩若干核心的技术要点。 在构建此类功能时,开发者通常会借助以下技术手段:1. **jQuery库**:jQuery是一个应用广泛的JavaScript框架,它简化了HTML文档的遍历、事件管理、动画效果以及Ajax通信。 在此项目中,jQuery将负责处理用户的点击动作(实现左右切换),并且制造流畅的过渡效果。 2. **图片轮播扩展工具**:开发者或许会采用现成的jQuery扩展,例如Slick、Bootstrap Carousel或个性化的轮播函数,以达成图片切换的功能。 这些扩展能够辅助迅速构建功能完善的轮播模块。 3. **即时数字呈现**:展示当前图片的序列号,这需要通过JavaScript或jQuery来追踪并调整。 每当图片切换时,相应的数字也会同步更新。 4. **CSS美化**:为了达成淘宝图片切换的视觉效果,可能需要设计特定的CSS样式,涵盖图片的排列方式、过渡效果、点状指示器等。 CSS3的动画和过渡特性(如`transition`和`animation`)在此过程中扮演关键角色。 5. **事件监测**:运用jQuery的`.on()`方法来监测用户的操作,比如点击左右控制按钮或自动按时间间隔切换。 根据用户的交互,触发相应的函数来执行...
垃圾实例分割数据集 一、基础信息 • 数据集名称:垃圾实例分割数据集 • 图片数量: 训练集:7,000张图片 验证集:426张图片 测试集:644张图片 • 训练集:7,000张图片 • 验证集:426张图片 • 测试集:644张图片 • 分类类别: 垃圾(Sampah) • 垃圾(Sampah) • 标注格式:YOLO格式,包含实例分割的多边形点坐标,适用于实例分割任务。 • 数据格式:图片文件 二、适用场景 • 智能垃圾检测系统开发:数据集支持实例分割任务,帮助构建能够自动识别和分割图像中垃圾区域的AI模型,适用于智能清洁机器人、自动垃圾桶等应用。 • 环境监控与管理:集成到监控系统中,用于实时检测公共区域的垃圾堆积,辅助环境清洁和治理决策。 • 计算机视觉研究:支持实例分割算法的研究和优化,特别是在垃圾识别领域,促进AI在环保方面的创新。 • 教育与实践:可用于高校或培训机构的AI课程,作为实例分割技术的实践数据集,帮助学生理解计算机视觉应用。 三、数据集优势 • 精确的实例分割标注:每个垃圾实例都使用详细的多边形点进行标注,确保分割边界准确,提升模型训练效果。 • 数据多样性:包含多种垃圾物品实例,覆盖不同场景,增强模型的泛化能力和鲁棒性。 • 格式兼容性强:YOLO标注格式易于与主流深度学习框架集成,如YOLO系列、PyTorch等,方便研究人员和开发者使用。 • 实际应用价值:直接针对现实世界的垃圾管理需求,为自动化环保解决方案提供可靠数据支持,具有重要的社会意义。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值