#pragma mark -
#pragma mark Table view data source
//tableView 有多少个 section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return [mutableArrayOrder count];
}
//每个 section 有几行(几个元素)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 1;
}
#pragma mark - tableView headView
//tableView 中 section 的头部 视图高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 40.0;
}
//tableView 中 section 的头部 视图
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
headView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
UIImage *image = [UIImage imageNamed:@"店铺.png"];
UIImageView *businessLogo = [[UIImageView alloc] initWithFrame:CGRectMake(10, 12, 15, 15)];
[businessLogo setImage:image];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, 150, 40)];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor darkGrayColor];
titleLabel.font = [UIFont systemFontOfSize:13];
UIImage *imageArrow = [UIImage imageNamed:@"右_02.png"];
UIImageView *arrowView = [[UIImageView alloc] initWithFrame:CGRectMake(200, 12, 15, 15)];
[arrowView setImage:imageArrow];
UILabel *orderTagLabel = [[UILabel alloc] initWithFrame:CGRectMake(headView.frame.size.width-60, 0, 150, 40)];
orderTagLabel.backgroundColor = [UIColor clearColor];
orderTagLabel.textColor = [UIColor darkGrayColor];
orderTagLabel.font = [UIFont systemFontOfSize:13];
if([mutableArrayOrder count] >0){
OrderEntity *entity = [mutableArrayOrder objectAtIndex:section];
titleLabel.text = entity.store_name;
orderTagLabel.text = entity.status;
}
[headView addSubview:businessLogo];
[headView addSubview:titleLabel];
[headView addSubview:arrowView];
[headView addSubview:orderTagLabel];
return headView;
}
#pragma mark - tableView footView
//tableView 中 section 的尾部 视图高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 90.0;
}
//tableView 中 section 的尾部 视图
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 90)];
footView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];//[UIColor blackColor];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width-10, 39)];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor darkGrayColor];
titleLabel.font = [UIFont systemFontOfSize:13];
titleLabel.textAlignment = NSTextAlignmentRight;
UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, 1)];
lineLabel.backgroundColor = [UIColor colorWithRed:0.95 green:0.93 blue:0.92 alpha:1];
UIButton *delOrder = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width-270, 45, 80, 30)];
[delOrder.layer setCornerRadius:5.0]; //设置矩形四个圆角半径
[delOrder.layer setBorderWidth:1.0]; //边框宽度
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 0.9, 0.9, 0.9, 1 });
[delOrder.layer setBorderColor:colorref];//边框颜色
[delOrder setTitle: @"删除订单" forState: UIControlStateNormal];
delOrder.titleLabel.font = [UIFont systemFontOfSize: 14.0];
// [delOrder setBackgroundColor: [UIColor blueColor]];
[delOrder setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
UIButton *getLogistics =[[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width-180, 45, 80, 30)];
[getLogistics.layer setCornerRadius:5.0]; //设置矩形四个圆角半径
[getLogistics.layer setBorderWidth:1.0]; //边框宽度
[getLogistics.layer setBorderColor:colorref];//边框颜色
[getLogistics setTitle: @"删除订单" forState: UIControlStateNormal];
getLogistics.titleLabel.font = [UIFont systemFontOfSize: 14.0];
// [getLogistics setBackgroundColor: [UIColor blueColor]];
[getLogistics setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
UIButton *orderComment =[[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width-90, 45, 80, 30)];
[orderComment.layer setCornerRadius:5.0]; //设置矩形四个圆角半径
[orderComment.layer setBorderWidth:1.0]; //边框宽度
[orderComment.layer setBorderColor:colorref];//边框颜色
[orderComment setTitle: @"去支付" forState: UIControlStateNormal];
orderComment.titleLabel.font = [UIFont systemFontOfSize: 14.0];
// [orderComment setBackgroundColor: [UIColor blueColor]];
[orderComment setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
UILabel *lineLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 80, self.view.frame.size.width, 1)];
lineLabel2.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1];
UILabel *lineLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(0, 81, self.view.frame.size.width, 10)];
lineLabel3.backgroundColor = [UIColor colorWithRed:0.95 green:0.93 blue:0.92 alpha:1];
if([mutableArrayOrder count] >0){
OrderEntity *entity = [mutableArrayOrder objectAtIndex:section];
titleLabel.text = [NSString stringWithFormat:@"%@%@%@%@%@",@"合计: ¥ ",entity.total_amount,@" (含运费: ¥ ",entity.ship_cost,@")"];
}
[footView addSubview:titleLabel];
[footView addSubview:lineLabel];
// [footView addSubview:delOrder];
[footView addSubview:getLogistics];
[footView addSubview:orderComment];
[footView addSubview:lineLabel2];
[footView addSubview:lineLabel3];
return footView;
}
//tableView 中组成的元素 cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//通过 Identifier 找到 cell 视图
static NSString *couponTableViewCellIdentifier=@"OrderTableViewCell";
OrderTableViewCell *cell = (OrderTableViewCell *)[tableView dequeueReusableCellWithIdentifier:couponTableViewCellIdentifier];
//防止 没有 cell 而导致 bug 报错 运行失败,使代码更加健全,安全,稳定,有效
if (cell == nil) {
NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"OrderTableViewCell" owner:self options:nil];
cell = [array objectAtIndex:0];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if([mutableArrayOrder count] > 0){
OrderEntity *entity = [mutableArrayOrder objectAtIndex:indexPath.row];
for (int i = 0; i < [entity.product_list count]; i++) {
OrderProductEntity *productEntity = [entity.product_list objectAtIndex:i];
cell.productName.text = productEntity.product_name;
[cell.productImage sd_setBackgroundImageWithURL:[NSURL URLWithString:productEntity.album_thumb] forState:UIControlStateNormal];
cell.introduce.text = productEntity.introduce;
cell.goodsCnt.text = [NSString stringWithFormat:@"%@%@",@"x ",productEntity.count];
cell.price.text = productEntity.price;
}
}
return cell;
}
#pragma mark 返回每组头标题名称
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return @"头";
}
#pragma mark 返回每组尾部说明
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
return @"尾";
}
//设置每行元素的 高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100;
}
#pragma mark -
#pragma mark Table view delegate
//tableVIew 每行元素的单击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
ps:
//以下三个函数是一定要在viewcontroller里实现的
(1)numberOfSectionsInTableView:
table 里有多少个 section
(2)tableView:numberOfRowsInSecion:
每个 section 需要加载多少个单元或多少行
(3)tableView:cellForRowAtIndexPath:
UITableViewCell 实例,用于构成 table view
TableViewController 使用,TableView 一些常用的方法
原文:http://my.oschina.net/jack088/blog/506464