UITableView 如何修改数据

来源:互联网 发布:好玩的网络竞技游戏 编辑:程序博客网 时间:2024/06/18 09:29



以下是截选出来一段 UITableViewController类

@interface ColorViewController : UITableViewController@end@implementation ColorViewController+ (id) controller    {ColorViewController *controller = [[ColorViewController alloc] init];    controller.title = @"Colors";return controller;}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{return 2;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{return 10;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"generic"];if (!cell) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"generic"];      // NSLog([NSString stringWithFormat:@"Cell%d%d", [indexPath section], [indexPath row]]);    cell.textLabel.text = @"Brightness";cell.textLabel.textColor = [UIColor colorWithWhite:(indexPath.row / 10.0f) alpha:1.0f]; //   cell.accessoryType = IS_IPAD ? UITableViewCellAccessoryNone : UITableViewCellAccessoryDisclosureIndicator;return cell;}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];    if (IS_IPAD){UIViewController *controller = (UIViewController *)self.splitViewController.delegate;controller.view.backgroundColor = cell.textLabel.textColor;}else {DetailViewController *controller = [DetailViewController controller];controller.view.backgroundColor = cell.textLabel.textColor;[self.navigationController pushViewController:controller animated:YES];}}- (void) viewDidAppear: (BOOL) animated{self.tableView.rowHeight = 72.0f;}- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{return YES;}@end

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 2;

}

定义  Section块 数量


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

{

return 10;

}

定义每个Section块中包含的行


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

{

UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:@"generic"];

if (!cell) cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"generic"];

    

  // NSLog([NSString stringWithFormat:@"Cell%d%d", [indexPath section], [indexPath row]]);

    

cell.textLabel.text =@"Brightness";

cell.textLabel.textColor = [UIColorcolorWithWhite:(indexPath.row / 10.0f) alpha:1.0f];

 //   cell.accessoryType = IS_IPAD ? UITableViewCellAccessoryNone : UITableViewCellAccessoryDisclosureIndicator;

return cell;

}


  // NSLog([NSString stringWithFormat:@"Cell%d%d", [indexPath section], [indexPath row]]);

  通过打印这句话。我们可以知道indexPath中存储的分别是 第几个Section 跟 第几个row


通过判断是哪个Section 跟哪个 row可以独立的修改  cell 的属性


cell.textLabel.text = NSString stringWithFormat:@"Cell%d%d", [indexPath section], [indexPath row]];


权当给我自己做注解了。。


原创粉丝点击