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

UITableView 实例详解 滑动编辑 headerView

2013-01-15 15:38 363 查看
self.dataArray = [[[NSMutableArray alloc]init] autorelease];
NSArray *array = [NSArray arrayWithObjects:[NSArray arrayWithObjects:@"Beijing",@"$500", nil],[NSArray arrayWithObjects:@"ShangHai",@"$510", nil],[NSArray arrayWithObjects:@"Guangzhou",@"$540", nil],[NSArray arrayWithObjects:@"Wuhan",@"$513", nil], nil];
[dataArray addObjectsFromArray:array];

int height = [UIScreen mainScreen].applicationFrame.size.height;
self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, height - 40) style:UITableViewStylePlain];
self.tableView.dataSource = self;
self.tableView.delegate = self;
[self.view addSubview:self.tableView];


点击 home button,进入编辑











纵向滑动tableCell 出现编辑按钮

#pragma mark - UITableViewDataSource/Delegate
//返回table 行数
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return dataArray.count;
}

- (UITableViewCell*) tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = @"cell";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
}

NSArray *data = [dataArray objectAtIndex:indexPath.row];

cell.textLabel.text = [data objectAtIndex:0];
cell.detailTextLabel.text = [data objectAtIndex:1];
cell.detailTextLabel.textColor = [UIColor redColor];

return cell;

}
//- (NSString*) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
//    NSString *headerString = @"Address                                   Price";
//    return headerString;
//}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
return @"删除";
}
//纵向滑动时响应,出现编辑按钮
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath{

NSLog(@"Start editing!");

}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{        //纵向滑动时出现编辑按钮
return YES;
}

//让行可以移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)_tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {      //编辑完响应事件
UITableViewCell *cell = [_tableView cellForRowAtIndexPath:indexPath];
NSLog(@"title:%@",cell.textLabel.text);
NSLog(@"price:%@",cell.detailTextLabel.text);

[dataArray removeObjectAtIndex:indexPath.row];
[tableView reloadData];

}
//设置table headerView
- (UIView*) tableView:(UITableView *)_tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, _tableView.frame.size.width, 20)] autorelease];
UILabel *leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, 20)];
leftLabel.textColor = [UIColor orangeColor];
UILabel *rightLabel = [[UILabel alloc]initWithFrame:CGRectMake(_tableView.frame.size.width - 100, 0, 90, 20)];
rightLabel.textColor = [UIColor orangeColor];

leftLabel.text = @"Address";
rightLabel.text = @"Price";
rightLabel.textAlignment = NSTextAlignmentRight;
leftLabel.backgroundColor = [UIColor clearColor];
rightLabel.backgroundColor = [UIColor clearColor];

UIImageView *backImage = [[UIImageView alloc]initWithFrame:headerView.frame];
backImage.image = [UIImage imageNamed:@"back.png"];

[headerView addSubview:backImage];

[headerView addSubview:leftLabel];
[headerView addSubview:rightLabel];

[leftLabel release];
[rightLabel release];
[backImage release];

return headerView;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: