uitableviewcell deselectrowatindexpath.row 方法是什么意思

iOS开发13:UITableView与UITableViewCell
UITableView用来以表格的形式显示数据。关于UITableView,我们应该注意:
(1)UITableView用来显示表格的可见部分,UITableViewCell用来显示表格的一行。
(2)UITableView并不负责存储表格中的数据,而是仅仅存储足够的数据使得可以画出当前可见部分。
(3)UITableView从UITableViewDelegate协议获取配置信息,从UITableViewDataSource协议获得数据信息。
(4)所有的UITableView实现时实际上只有一列,但是我们可以通过向UITableViewCell中添加子视图,使得它看起来有好几列。
(5)UITableView有两种风格:
&&& ①
Grouped:每一组看起来像是圆矩形;
&&& ②
Plain:这是默认风格,可以修改成Indexed风格。
在下边的小例子中,我们将先实现显示一列数据,然后在每行添加图像,之后再看看UITableViewCell的四种分别是什么样的。最后再进行其他操作,比如设置缩进、修改字体大小和行高等。
1、运行Xcode 4.2,新建一个Single View Application,名称为Table Sample:
2、单击ViewController.xib,使用Interface
Builder给视图添加一个UITableView控件,并使其覆盖整个视图:
3、选中新添加的UITableView控件,打开Connection
Inspector,找到delegate和datasource,从它们右边的圆圈拉线到File's Owner图标:
4、单击ViewController.h,在其中添加代码:
#import &UIKit/UIKit.h&
@interface ViewController : UIViewController&UITableViewDelegate, UITableViewDataSource&
@property (strong, nonatomic) NSArray *listD
5、单击ViewController.m,在其中添加代码:
5.1 在@implementation后面添加代码:
@synthesize listD
5.2 在viewDidLoad方法中添加代码:
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSArray *array = [[NSArray alloc] initWithObjects:@"Tree", @"Flower",
@"Grass", @"Fence", @"House", @"Table", @"Chair",
@"Book", @"Swing" , nil];
self.listData =
5.3 在viewDidUnload方法中添加代码:
- (void)viewDidUnload
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet =
self.listData =
5.4 在@end之前添加代码:
#pragma mark -
#pragma mark Table View Data Source Methods
//返回行数
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return [self.listData count];
//新建某一行并返回
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *TableSampleIdentifier = @"TableSampleIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
TableSampleIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:TableSampleIdentifier];
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
上面的第二个方法中,
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: TableSampleIdentifier];
这个语句根据标识符TableSampleIdentifier寻找当前可以重用的UITableViewCell。当某行滑出当前可见区域后,我们重用它所对应的UITableViewCell对象,那么就可以节省内存和时间。
如果执行词语后,cell为nil,那我们再创建一个,并设置去标识符为TableSampleIdentifier:
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableSampleIdentifier];
这里UITableViewCellStyleDefault是表示UITableViewCell风格的常数,除此之外,还有其他风格,后面将会用到。
注意参数(NSIndexPath *)indexPath,它将行号row和部分号section合并了,通过[indexPath
row];获取行号。之后给cell设置其文本:
cell.textLabel.text = [listData objectAtIndex: row];
6、运行一下:
7、给每一行添加图片:
7.1 将图片资源添加到工程:拖到工程中,前面的文章有提到。
7.2 在cellForRowAtIndexPath方法的return语句之前添加代码:
UIImage *image = [UIImage imageNamed:@"blue.png"];
cell.imageView.image =
UIImage *highLighedImage = [UIImage imageNamed:@"yellow.png"];
cell.imageView.highlighedImage = highLighedI
7.3 运行,效果如下:
可以看到,每行左边都出现一张图片。当选中某行,其图片改变。
8、设置行的风格:
表示UITableViewCell风格的常量有:
UITableViewCellStyleDefault
UITableViewCellStyleSubtile
UITableViewCellStyleValue1
UITableViewCellStyleValue2
这几种风格的区别主要体现在Image、Text Label以及Detail Text Label。
为了体现风格,在cellForRowAtIndexPath方法的return语句之前添加代码:
cell.detailTextLabel.text = @"Detail Text";
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableSampleIdentifier];
中的UITableViewCellStyleDefault依次换成上面提到的四个风格常量,并运行,效果分别如下:
&&&&&&&&&&&
UITableViewCellStyleDefault&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&UITableViewCellStyleSubtle
&&&&&&&&&&&&
UITableViewCellStyleValue1&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
UITableViewCellStyleValue2
9、设置缩进:
将所有行的风格改回UITableViewCellStyleDefault,然后在@end之前添加代码如下:
#pragma mark Table Delegate Methods
- (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
这里将第row行缩进row,如下图所示:
10、操纵行选择:
在@end之前添加代码:
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
if (row%2 == 0) {
return indexP
上面的方法在选择某行之前执行,我们可以在这个方法中添加我们想要的操作。这里,我们实现的是,如果选择的行号(从0开始计)是偶数,则取消选择。
在@end之前添加代码:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
NSString *rowValue = [listData objectAtIndex:row];
NSString *message = [[NSString alloc] initWithFormat:
@"You selected %@", rowValue];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Row Selected!"
message:message
delegate:nil
cancelButtonTitle:@"Yes I Did"
otherButtonTitles:nil];
[alert show];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
当选择某行之后,就弹出一个Alert,用来显示我们所做的选择。
运行一下,你会发现第0、2等行无法选择。选择奇数行时会弹出提示:
而且关闭提示框后,选择的那行也被取消了选择,用的语句
[tableView deselectRowAtIndexPath:indexPath animated:YES];
11、设置字体大小和表格行高:
11.1 在cellForRowAtIndexPath方法中的return之前添加代码,用于设置字体和大小:
cell.textLabel.font = [UIFont boldSystemFontOfSize:50];
11.2 在@end之前添加代码,用于设置行高:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 70;
运行,看看效果:
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。UITableView的使用
如何使用UITabelView
要使用UITableView,就要我们自定义的类继承于UITableViewController(IOS中的MVC设计模式),
然后我们就可以继承处理UITableView的各种方法了。Apple对方法定义名很好理解,看到方法名也就
知道起什么作用了。
一些主要的方法
UITableView主要用于相同数据的展示,是UITabelViewCell的集合,每个UITableViewCell用来展示数据。
为了定位每个UITableViewCell,又有了section,row2个变量,具体的如下图所示
主要的方法:
返回section的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
设置每个section的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
返回每个section中row的个数,其实就是UITableViewCell的个数。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
处理每个UITableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
每个UITabelViewCell如何展示,indexPath包含了UITableView的section,row信息,可以通过indexPath.section,indexPath.row
返回每个UITableViewCell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
UITableViewCell的选中事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
滑动删除的事件
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
滑动删除 的 删除按钮
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0){
return @&删除&;
取消选择效果
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)
每个UITableViewCell点击以后的事件,push一个新的view,刷新数据?在这里添加下处理就OK。
根据数据来确定section,row的值,用来展示数据,只要确定好section,及row的内容。
处理每个cell
我们就可以根据数据在每个cell中添加我们想要的处理。看一下在药品指南中的代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @&Cell&;
UITableViewCell
*cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
// 设置cell无选中效果
cell.selectionStyle = UITableViewCellSelectionStyleN
// 初始化tableview的内容
if (indexPath.section!=0) {
CGSize size = [self getLabeSize:[_theDic objectForKey:[_theArray objectAtIndex:indexPath.section - 1]] Width:300];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, size.width, size.height+10)];
[self initLabel:label];
[label setText:[_theDic objectForKey:[_theArray objectAtIndex:indexPath.section -1]]];
[cell.contentView addSubview:label ];
。。。。。。。。。。。
。。。。。。。。。。。
根据特定的条件,来初始化UITableViewCell。可以自定义UILabel,UIButton,UIImageView等,添加到UITableViewCell上。
一切取决于你的数据特点,以及你想要的效果。
比如说实现UITableView隔行变色,只要判断indexPath.row的值,然后设置cell的背景色就行,如果你动态添加了UILabel等之类的,也要
做相应的颜色变换。
cell.selectionStyle = UITableViewCellSelectionStyleN
用来设置cell没有选中的效果。
滑动删除的实现
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
//处理数据,然后刷新tableview。
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
导航栏右上角添加编辑按钮实现删除
添加按钮,并绑定函数
UIBarButtonItem *editButton = [[UIBarButtonItem alloc]
initWithTitle:@&编辑&
style:UIBarButtonItemStyleDone
target:self
action:@selector(edit)];
self.navigationItem.rightBarButtonItem = editB
[editButton release];
_tag = YES;
-(void) edit{
if (_tag == YES) {
[self.tableView setEditing:YES animated:YES];
//设置导航栏上右边的编辑按钮
UIBarButtonItem *editButton = [[UIBarButtonItem alloc]
initWithTitle:@&完成&
style:UIBarButtonItemStyleBordered
target:self
action:@selector(edit)];
self.navigationItem.rightBarButtonItem = editB
_tag = NO;
[self.tableView setEditing:NO animated:YES];
//设置导航栏上右边的编辑按钮
UIBarButtonItem *editButton = [[UIBarButtonItem alloc]
initWithTitle:@&编辑&
style:UIBarButtonItemStyleBordered
target:self
action:@selector(edit)];
self.navigationItem.rightBarButtonItem = editB
_tag = YES;
快速定位的实现
UITableView自带了快速索引的办法,我们要做的是处理好数据,获得索引序列,以及对应内容。
3个重要的方法
返回的是索引的序列。
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
初始化每个section的title,
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
返回的是section title集合中与索引序列字母相同的section的位置。
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
处理好数据,然后就可以实现快速定位。
TableView与其他控件共存
经常在一个页面中,需要在上部显示一些控件,如:搜索框等,下部显示TableView。
对于这种情况,即使用ViewController即可,最底层是个普通的View,在View上分别添加搜索框与TableView。
在xib上,代码中都可以做到。需要注意,对于TableView的delegate与dataSource需要设置为self,在xib上直接拉线即可,在代码中,直接
_evaTable = [[UITableView alloc] initWithFrame:CGRectMake(0,70,320,346)];//UITableViewStyleGrouped
_evaTable.delegate =
_evaTable.dataSource =
[self.view addSubview:_evaTable];
添加自定义的背景图片
UIImageView *imageview = [[UIImageView alloc] initWithFrame:self.myTable.backgroundView.frame];
[imageview setImage:[UIImage imageNamed:@&bb2.png&]];
self.myTable.backgroundView =
还要设置uitableviewcell的背景色为透明色。
这样tableview背景图使我们自定义的图片
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:42604次
排名:千里之外
转载:43篇
(1)(2)(8)(2)(11)(8)(3)(1)(13)//最先进的是这个
返回一共有多少行!
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
NSLog(@"111");
tableView.contentInset = UIEdgeInsetsMake(0, 0, -144, 0);
//AppDelegate为一个代理类。
AppDelegate *app = [[UIApplication sharedApplication]delegate];
int count = [app.module.arrlist count];
if (count & 4)
return count+2;
//然后就进到了这里面。
这次返回的是每一行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
NSLog(@"222");
return 55;
//这是关键一步。只要有滑动,如果超过了它的范围,它就会一直调用这个方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
NSLog(@"333");
AppDelegate *app = [[UIApplication sharedApplication]delegate];
if(indexPath.row & app.module.arrlist.count)
UITableViewCell *cell = [[UITableViewCell alloc]initWithFrame:CGRectZero];
cell.frame = CGRectMake(0, 0, 320, 50);
UILabel *label = [[UILabel alloc]init];
label.frame = CGRectMake(0, 0, 320, 30);
label.text = @"aaaaaa";
[cell.contentView addSubview:label];
label.text = [[app.module.arrlist objectAtIndex:indexPath.row]objectForKey:@"Name"];
else if(indexPath.row == app.module.arrlist.count)
UITableViewCell *cell1 = [[UITableViewCell alloc]initWithFrame:CGRectZero];
cell1.frame = CGRectMake(0, 0, 320, 50);
UILabel *label1 = [[UILabel alloc]init];
label1.frame = CGRectMake(0, 0, 320, 30);
label1.text = @"刷新";
[cell1.contentView addSubview:label1];
return cell1;
//[self waitView:YES];
NSLog(@"~~~~~~~~~~~~~~");
[self waitView:YES];
//NSLog(@"%@",[testarray objectAtIndex:0]);
UITableViewCell *cell1 = [[UITableViewCell alloc]initWithFrame:CGRectZero];
cell1.frame = CGRectMake(0, 0, 320, 50);
UILabel *label1 = [[UILabel alloc]init];
label1.frame = CGRectMake(0, 0, 320, 30);
label1.text = @"刷新~";
[cell1.contentView addSubview:label1];
//当数据到达末尾的时候,这时候就需要重新为加载数据了。加载前的行数为10,第一次加载后它的行数恋为了22,这样它就会一直循环下去
for(i =0; i&10; i++)
[app.module.arrlist addObject:app.module.arrinfo];
NSLog(@"count%d",app.module.arrlist.count);
[tbv reloadData];
[self stop];
return cell1;
//点击单个table view 的时候为触发这个方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
NSLog(@"4444");
[tableView deselectRowAtIndexPath:indexPath animated:NO];
-(void)xmlarraytest
AppDelegate *app = [[UIApplication sharedApplication]delegate];
app.module.arrinfo = [NSMutableDictionary dictionary];
app.module.arrlist = [NSMutableArray array];
//[app.module.arrinfo setObject:@"TTTTTT" forKey:@"key"];
NSMutableDictionary *dic1 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"Michael", @"Name", @"26", @"Age", nil];
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:dic1, dic1, dic1, dic1, dic1,dic1,dic1,dic1,dic1,dic1,nil];
app.module.arrinfo = dic1;
app.module.arrlist =
[app.module.arrlist addObject:app.module.arrinfo];
NSLog(@"qq%d",app.module.arrlist.count);
NSLog(@"xxx%@",[app.module.arrlist objectAtIndex:0]);
NSLog(@"%@",[app.module.arrinfo objectForKey:@"Name"]);
NSLog(@"app:%@",[[app.module.arrlist objectAtIndex:0]objectForKey:@"Name"]);
//圆圈动画
_aiView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
//[aiView setBackgroundColor:[UIColor grayColor]];
_aiView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2 - 45/2, [UIScreen mainScreen].bounds.size.height/2 - 45/2+120, 45, 45);
_aiView.hidesWhenStopped = YES;
[self.view addSubview:[_aiView autorelease]];
[_aiView stopAnimating];
//圈圈动画控制
-(void)waitView:(BOOL)want2Show
if (want2Show) {
[self.view bringSubviewToFront:_clickMask];
[self.view bringSubviewToFront:_aiView];
[_clickMask setHidden:NO];
[_aiView startAnimating];
[_clickMask setHidden:YES];
[_aiView stopAnimating];
[self waitView:YES];
阅读(...) 评论()1824人阅读
1.首先,重定义一个uitableviewcell,它自动生成的代码不需要动,只要在.m文件中再加上下面的代码即可:
(具体可参看:http://blog.csdn.net/zhangjie1989/article/details/7550990)
- (void)layoutSubviews {
& & [superlayoutSubviews];
& & self.imageView.bounds =CGRectMake(0,0,44,44);
& & self.imageView.frame =CGRectMake(0,0,44,44);
& & self.imageView.contentMode =UIViewContentModeScaleAspectFit;
& & CGRect tmpFrame =
self.textLabel.frame;
& & tmpFrame.origin.x = 46;
& & self.textLabel.frame = tmpF
& & tmpFrame = self.detailTextLabel.frame;
& & tmpFrame.origin.x = 46;
& & self.detailTextLabel.frame = tmpF & &
2.关于选中uitableviewcell后想让选中状态消失的实现方法:
/bbs/simple/?t123810.html
一句代码搞定[tableView deselectRowAtIndexPath:indexPath animated:YES];
如果想让返回时蓝色消失,就在viewWillAppera里面 通过[myTableView indexPathForSelectedRow]获取选择的cell的IndexPath
然后调用楼上的方法。如果想点完就消失,就直接在didSelected方法里面加、
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:451773次
积分:8530
积分:8530
排名:第748名
原创:357篇
转载:430篇
评论:30条
(3)(17)(35)(10)(24)(15)(8)(28)(13)(11)(28)(26)(20)(43)(55)(13)(14)(25)(12)(19)(37)(64)(29)(31)(7)(30)(12)(45)(19)(17)(23)(8)(11)(36)}

我要回帖

更多关于 nsindexpath 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信