TableViewController 使用,TableView 一些常用的方法

#pragma mark -
#pragma mark Table view data source
//tableView 有多少个 section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
}

//每个 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 点击时没有灰色背景
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //设置 cell 右侧显示 箭头
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    //cell没有任何的样式
    //cell.accessoryType = UITableViewCellAccessoryNone;
    //cell右边有一个蓝色的圆形button;
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    //cell右边的形状是对号; 
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    
    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 {
    
}

// 设置 cell 可以编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

//提交 每行 cell 的具体操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [mutableArrayAddress removeObjectAtIndex:indexPath.row];
        // Delete the row from the data source.
        [tableViewAddress deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        
        //删除和添加单元格中用到 UITableViewRowAnimation 动画效果,它还有其他几种效果
        //UITableViewRowAnimationAutomatic      UITableViewRowAnimationTop 
        //UITableViewRowAnimationBottom         UITableViewRowAnimationLeft
        //UITableViewRowAnimationRight          UITableViewRowAnimationMiddle
        //UITableViewRowAnimationFade           UITableViewRowAnimationNone
        
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }
}

//修改 左滑 显示 “删除” 按钮的文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{ 
        return @"TEST"; 
}

//当在Cell上滑动时会调用此函数
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  UITableViewCellEditingStyleDelete;  //返回此值时,Cell会做出响应显示Delete按键
    //return UITableViewCellEditingStyleNone;
    //return UITableViewCellEditingStyleInsert;
}

//滑动操作前 调用该函数
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath*)indexPath{
}

//滑动操作后 调用该函数
- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath*)indexPath{
}

//允许单元格的移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

 

ps:

//以下三个函数是一定要在viewcontroller里实现的
(1)numberOfSectionsInTableView:
   table 里有多少个 section 
(2)tableView:numberOfRowsInSecion:
   每个 section 需要加载多少个单元或多少行
(3)tableView:cellForRowAtIndexPath:

   UITableViewCell 实例,用于构成 table view

//手动调用 tableView 的点击事件

[self tableView:_tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];

//不显示多余的空的 cell

self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

 

 

// 调用deleteRowsAtIndexPaths:withRowAnimation:withRowAnimation:闪退

因为 tableView:numberOfRowsInSection 返回的行数不是删除前-1则会crash

同样删除最后一行导致 numberOfSectionsInTableView 返回值跟调用前不一样而crash。

解决方法:

调用 deleteRowsAtIndexPaths:withRowAnimation: 时判断是不是该section最后一行,是的话则同时调用deleteSections:indexSetWithIndex:withRowAnimation:

//点击删除按钮的响应方法,在这里处理删除的逻辑

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

        if editingStyle == UITableViewCellEditingStyle.delete {

            // TODO 输出操作

            self.devices.remove(at: indexPath.row)

            if(self.devices.count == 0){

                self.tableView.deleteSections(NSIndexSet(index: indexPath.section) as IndexSet, with: UITableViewRowAnimation.fade)

            }else{

                self.tableView!.deleteRows(at: [indexPath], with: UITableViewRowAnimation.fade)

            }

        }

    }

http://www.jianshu.com/p/284fd7d8c9e9


 

 

转载于:https://my.oschina.net/jack088/blog/506464

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值