IOS-UITableView入门(2)

1.对于TableView ,每个item的视图基本都是一样的。不同的只有数据。

IOS提供了一种缓存视图跟数据的方法。在 -UITableViewCell *) tableView:cellForRowAtIndexPath:

//创建一个用于缓存的标示
    static NSString *[email protected]"CellTable";
    //先从缓存中取得UITableViewCell
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
    //如果取不到,则代码创建。
    if (cell==nil) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }

2

//创建系统提供的每个item右边的图标
        cell.accessoryType=UITableViewCellAccessoryCheckmark;

3.通过动画修改某个item的显示

[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];

4.通过动画删除某个item

[self.tableView deleteRowsAtIndexPaths:self.indexPaths withRowAnimation:UITableViewRowAnimationLeft];

CSZViewController.h 整体代码如下:声明TableView的协议和数据源

#import <UIKit/UIKit.h>

@interface CSZViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

- (IBAction)trashClick:(id)sender;

@end

CSZViewController.m 有关TableView代码如下:

#pragma mark - dataSource
#pragma mark 每列行数
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}

#pragma mark 创建每行的View
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //创建一个用于缓存的标示
    static NSString *[email protected]"CellTable";
    //先从缓存中取得UITableViewCell
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
    //如果取不到,则代码创建。
    if (cell==nil) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    cell.textLabel.text=self.array[indexPath.row];
    [email protected]"description....";

    if ([self.deleteArr containsObject:cell.textLabel.text]) {
        //创建系统提供的每个item右边的图标
        cell.accessoryType=UITableViewCellAccessoryCheckmark;
    }else
    {
        cell.accessoryType=UITableViewCellAccessoryNone;
    }

    return  cell;

}

#pragma mark - UITableViewDelegate
#pragma mark 点击每个item调用
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if ([self.deleteArr containsObject:self.array[indexPath.row]]) {
        [self.deleteArr removeObject:self.array[indexPath.row]];
        [self.indexPaths removeObject:indexPath];
    }else
    {
        [self.deleteArr addObject:self.array[indexPath.row]];
        [self.indexPaths addObject:indexPath];
    }
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}

#pragma mark 返回每行高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

IOS-UITableView入门(2)

时间: 2025-01-02 09:20:43

IOS-UITableView入门(2)的相关文章

ios UItableView,UITableViewHeaderFooterView分组头部的重用机制,简单地仿射变换CGAffineTransform

怎样设置包括第一栏在内相同高度的section(小技巧,虽然容易但容易忽略) *第一步,在viewdidload里将尾部设为0,table.sectionFooterHeight = 0;(代理方法)- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return 0; }虽然也可以设置尾部高度,但是设置后没有效果 第二步,调用tableView的代理方法- (CGF

iOS开发入门教程_iOS开发视频教程

iOS开发入门教程 (Object-C.网络编程.多线程.蓝牙.二维码.Cocos2D.OpenGL)适合人群:初级课时数量:34课时用到技术:IOS,Object-C,OpenGL,XCode,Cocos 2D涉及项目:Cocos+2D.Game Kit蓝牙数据处理等咨询QQ:1840215592 iOS开发入门教程详细查看:http://www.ibeifeng.com/goods-471.html1.1.课程目标iOS开发入门教程内容的目标是初学者入门,让入门者提高,让所有人符合企业招聘的

IOS UITableView NSIndexPath属性讲解

IOS UITableView NSIndexPath属性讲解 查看UITableView的帮助文档我们会注意到UITableView有两个Delegate分别为:dataSource和delegate. dataSource 是UITableViewDataSource类型,主要为UITableView提 供显示用的数据(UITableViewCell),指定UITableViewCell支持的编辑操作类型(insert,delete和 reordering),并根据用户的操作进行相应的数据更

【IOS】IOS快速入门之OC语法

Objective-C 是 C 语言的超集 您还可以访问标准 C 库例程,例如在 stdlib.h 和 stdio.h 中声明的那些例程. Objective-C 还是一种非常动态的程序设计语言,而且这种动态是其最大优势.这种动态体现在它允许在运行应用程序时(即运行时)才去确定其行为,而不是在生成期间就已固定下来.因此,Objective-C 的动态机制让程序免受约束(编译和链接程序时施加的约束):进而在用户控制下,将大多数符号解析责任转移到运行时. 当您想要在源代码中包括头文件时,请在头文件或

iOS——UITableView数据源的一些问题

UITableView数据源的问题"name=image_operate_1771416366029362alt="iOS UITableView数据源的问题"src="http://simg.sinajs.cn/blog7style/images/common/sg_trans.gif"real_src="http://s15.sinaimg.cn/bmiddle/002WA StNzy6NJcgxGSafe&690">

IOS UItableView得group风格如何去掉分割线问题

在自定义UItableView的时候,当选择的style为Group时,往往在设置透明后分割线还在,为了去除,只要重新设置一个BackgroundView覆盖掉原来的即可 //取消分割线 UIView *view= [ [ [ UIView  alloc ] init ] autorelease]; [cell setBackgroundView :view]; //取消点击效果 cell.selectionStyle = UITableViewCellSelectionStyleNone; I

IOS UITableView Group&amp;Section

UItableView 根据数据结构不同 会有不同样式 关键在两个代理 tableviewdelegate&tabledatasourse 下面代码是我实施的Group 在模拟器中 ios6.1和ios7 并且滚动后相应的section会“置顶”,效果不错哦! 核心代码: #import <UIKit/UIKit.h> @interface AnnouncementViewController : UIViewController<UITableViewDataSource,UI

ios UITableView 相关

tableView 实现的方法 无分组的cell #pragma mark - Table view data source - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.contacts.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRow

iOS UITableView划动删除的实现

标签:划动删除 iphone 滑动删除 ios UITableView 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://rainbird.blog.51cto.com/211214/634587 从七八月前对苹果一无所知,到现在手持iphone,ipad,itouch有三个线上成熟app并熟练开发ios应用.一路走来一直站在前辈的肩膀上不断进步.如今生活工作稳定是时候将一直以来的一些心得整理出来了.想来想去决定先说说UITab

iOS开发入门——17条 Swift 最佳实践规范(下)

文章来源:http://www.zretc.com/technologyDetail/433.html 承接上文:iOS开发入门--17条 Swift 最佳实践规范(上) 9.单例(Singletons) 在Swift中单例是很简单的: class ControversyManager { static let sharedInstance = ControversyManager()} Swift 的 runtime 会保证单例的创建并且采用线程安全的方式访问. 单例通常只需要访问"share