UITableViewController

UITableViewController 列表在iPhone开发中起着决定性的重要作用,但是UITableViewController并不是那么简单使用的,以下就是其中的重要方法和Delegate:

//这个delegate会获取有多少个"章节"

-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{

      return1;//这里返回的是章节(section)的个数

       //如果数据源是一个复杂array或dictionary,则可以返回

    return[NSArray count]; 作为章节的个数

}


 //这个delegate会获取章节内有多少行

-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{

        return 0; //这个部分是必须要改的,否则0行的话,就是一个空表了

      //如果数据源是一个复杂array或dictionary, 可以使用嵌套的查询来返回这个个数了//比如:

       return [[NSArray objectAtIndex:section]//section为整数,所以如果使用NSDictionary,

     //记得使用辅助方法来计算其索引的key,或嵌套查询

       count];

      //因为返回的依旧是一个array或dictionary对象,所以我们获取它的大小count

 }


// 这里编辑每个栏格的外观

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{ 

    staticNSString*CellIdentifier=@"Cell"

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell== nil){

       cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

     } 

    // 一般我们就可以在这开始设置这个cell了,比如设置文字等:

    cell.textField.text = [NSArray objectAtIndex:indexPath.row];//假设这个NSArray就是本section的数据了,否则需要嵌套来查询section的信息,如前一个delegate       

    return cell;

   }

}

    //当栏格被点击后需要触发的事件

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

    AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];

    // 建立并使用本地导航管理推入视图控制器是最普遍的方法

    [self.navigationController pushViewController:anotherViewController];

    [anotherViewController release];

    }

    // 该delegate是可选的,对那些可以被编辑的对象返回YES

-(BOOL)tableView:(UITableView*)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath{

    // Return NO if you do not want the specified item to be editable.

    returnYES;

}

    // 对特定编辑风格进行操作

-(void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath{

    //以下是apple的缺省实例

    if(editingStyle== UITableViewCellEditingStyleDelete){

    // 如果是要删除那行栏格,那么就去删除数据源中的对象

    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];

    }elseif (editingStyle== UITableViewCellEditingStyleInsert){

 // 如果是要添加栏格,那么就应该将要添加的内容添加到数据源中

   }}

  // 可选delegate,对那些被移动栏格作特定操作

-(void)tableView:(UITableView*)tableView moveRowAtIndexPath:(NSIndexPath*)fromIndexPath toIndexPath:(NSIndexPath*)toIndexPath{}

// 对那些可以移动的行返回YES

-(BOOL)tableView:(UITableView*)tableView canMoveRowAtIndexPath:(NSIndexPath*)indexPath{

     // 如果不像让栏格移动,就返回NO

    returnYES;

}


禁止TableView滚动,

 

self.tableView.scrollEnabled = NO;

或者

tableView.userInteractionEnabled = NO;

 

弹出模式视图根导航

    TagetView *myTagetView= [[TagetView alloc] initWithNibName:@"TagetView" bundle:nil];
    myTagetView.modalTransitionStyle=UIModalTransitionStyleCrossDissolve;//淡出效果

       UIModalTransitionStyleCoverVertical,//上升效果
       UIModalTransitionStyleFlipHorizontal,//翻转效果

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:myTagetView];   

[self.navigationController presentModalViewController:navController animated:YES];  //弹出模式控制视图

[myTagetView release];   

[navController release];

 

[self.navigationController dismissModalViewControllerAnimated:YES] ;//做掉模式视图,和导航入栈的所有视图控制类


view上添加了一个tableView,但其样式是默认的,其中的内容也是空白的,而且此时是无法运行的,因为在头文件中添加了UITableViewDataSource和UITableViewDelegate两个类,所以必须设置一些自定义tableView样式的方法,下面列举了一些相关的方法:


设置Cell高度:

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



设置SectionHeader高度:

-(CGFloat)tableView:(UITableView *)tableViewheightForHeaderInSection:(NSInteger)section



设置SectionFooter高度:

-(CGFloat)tableView:(UITableView *)tableViewheightForFooterInSection:(NSInteger)section



设置Section数目:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView


 

设置SectionHeader内容:

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



设置各个Section中的Cell个数: 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section



设置Cell内容: 

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

 



设置Cell行缩进量:

-(NSInteger)tableView:(UITableView *)tableViewindentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath



设置Cell被选中响应前动作(例如:可用以判断选中的Cell,来阻止其响应)

-(NSIndexPath*)tableView:(UITableView*)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath



设置Cell选中触发响应:

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



---------------------

  1. //一个section刷新    
  2. NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:2];    
  3. [tableview reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];    
  4. //一个cell刷新    
  5. NSIndexPath *indexPath=[NSIndexPath indexPathForRow:3 inSection:0];    
  6. [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone]; 







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值