您的位置:首页 > 产品设计 > UI/UE

UITableView 如何修改数据

2013-11-10 22:37 260 查看
以下是截选出来一段 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 = [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;
}

  // 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]];

权当给我自己做注解了。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息