登录 立即注册
金钱:

Code4App-iOS开发-iOS 开源代码库-iOS代码实例搜索-iOS特效示例-iOS代码例子下载-Code4App.com

仿钉钉报销+cell+textview+自适应

[复制链接]
来自: zl313419 分类: iOS精品源码 上传时间: 2016-8-24 17:55:54
Tag:列表 丁丁 cell 自适应高度 height

项目介绍:

第一次上传写的代码,没有封装,逻辑简单,代码不是很好看,大神勿喷!有好的建议可以及时提,我会继续努力的!!


主要实现代码:
[Objective-C] 查看源文件 复制代码
#import "ViewController.h"
#import "ExpenseDemoCell.h"
#import "IQKeyboardManager.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UITextViewDelegate,TextViewCellDelegate>
{
    UITableView *TableView;
    
    UIScrollView *MainSc;
    
    UIView *MoneyView;
}
@property (nonatomic, strong) NSMutableArray *data;

@end

@implementation ViewController

//屏幕大小
#define kDeviceHeight [UIScreen mainScreen].bounds.size.height
#define kDeviceWidth [UIScreen mainScreen].bounds.size.width

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [IQKeyboardManager sharedManager].enable = YES;
    
}

- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    
    [IQKeyboardManager sharedManager].enable = YES;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];

    
    MainSc=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kDeviceWidth, kDeviceHeight)];
    MainSc.backgroundColor=[UIColor lightGrayColor];
    MainSc.contentSize=CGSizeMake(0, 1000);
    [self.view addSubview:MainSc];
    
    
    TableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, kDeviceWidth, 220) style:UITableViewStyleGrouped];
    TableView.delegate=self;
    TableView.dataSource=self;
    TableView.scrollEnabled = NO;

    TableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0,0,0,0.01)];

    [MainSc addSubview:TableView];
    
    [TableView registerNib:[UINib nibWithNibName:@"ExpenseDemoCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    
    NSArray *arr = @[@{@"aliph":@"0",
                       @"type":@"",
                       @"Money":@"",
                       @"ExpenseType":@"",
                       @"ExpenseDetails":@"cell1"}];

    self.data = [[NSMutableArray alloc]initWithArray:arr];
    

    MoneyView=[[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(TableView.frame), kDeviceWidth, 40)];
    MoneyView.backgroundColor=[UIColor whiteColor];
    [MainSc addSubview:MoneyView];
    
    UILabel *label1=[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 20)];
    label1.text=@"总报销金额(元):";
    label1.font=[UIFont systemFontOfSize:14];
    [MoneyView addSubview:label1];
    

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *inforStr = self.data[indexPath.row];
    
    
    UIFont *font = [UIFont systemFontOfSize:14];
    CGSize size = CGSizeMake(kDeviceWidth-80,2000);
    CGSize labelsize = [inforStr[@"ExpenseDetails"] sizeWithFont:font constrainedToSize:size lineBreakMode:NSLineBreakByCharWrapping];
    
    return labelsize.height+163;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.data.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ExpenseDemoCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
//    if (!cell) {
//        cell=[[ExpenseDemoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
//    }
    
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(touch)];
    [cell.contentView addGestureRecognizer:tap];
    
    cell.contentView.userInteractionEnabled=YES;
    
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    cell.delegate=self;
    
    [cell initwith:self.data[indexPath.row] Index:indexPath.row+1];
    
    
    
    return cell;
    
}
//设置表尾高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    static NSString *footSectionID = @"footSectionID";
    UITableViewHeaderFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:footSectionID];
    footerView.backgroundColor=[UIColor redColor];
    
    UIButton *add;
    
    if (footerView == nil) {
        footerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:footSectionID];
        add = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, 40)];
        add.layer.borderWidth=1;
        add.layer.borderColor=[[UIColor blackColor] CGColor];
//      label.font = FONT(13);
        
        [footerView addSubview:add];
        
        [add setTitle:@"+增加报账明细" forState:UIControlStateNormal];
        [add setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [add addTarget:self action:@selector(CreatNew) forControlEvents:UIControlEventTouchUpInside];
    }

    return footerView;
}

-(void)touch
{
    [self.view endEditing:YES];
    
    TableView.frame=CGRectMake(0, 0, 320, 568);
}

- (void)textViewCell:(ExpenseDemoCell *)cell didChangeText:(NSString *)text
{
    NSIndexPath *indexPath = [TableView indexPathForCell:cell];
    
    NSMutableDictionary *dict=[[NSMutableDictionary alloc]initWithDictionary:self.data[indexPath.row]];
    
    dict[@"ExpenseDetails"]=text;
    
    self.data[indexPath.row]=dict;
    
    
    TableView.frame=CGRectMake(0, 0, kDeviceWidth, TableView.contentSize.height+19);
//
    MoneyView.frame=CGRectMake(0, CGRectGetMaxY(TableView.frame), kDeviceWidth, 40);
//
//    //    NSLog(@"%f",TableView.contentSize.height);
//    //
    MainSc.contentSize=CGSizeMake(0, CGRectGetMaxY(TableView.frame)+40);
    
}
-(void)CreatNew
{
    [self.data addObject:@{@"aliph":@"1",
                           @"type":@"删除",
                           @"Money":@"",
                           @"ExpenseType":@"",
                           @"ExpenseDetails":@"cell2"}];
    
    [TableView reloadData];
    
    TableView.frame=CGRectMake(0, 0, kDeviceWidth, TableView.contentSize.height);
    
    MoneyView.frame=CGRectMake(0, CGRectGetMaxY(TableView.frame), kDeviceWidth, 40);
    
//    NSLog(@"%f",TableView.contentSize.height);
//    
    MainSc.contentSize=CGSizeMake(0, CGRectGetMaxY(TableView.frame)+40);
    

}
#pragma make-----代理
-(void)Refresh:(NSInteger)index
{
    
    UIAlertController *alert=[UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"你确定要删除报销明细%ldma",index] message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    
    UIAlertAction *ok=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
        [self.data removeObjectAtIndex:index-1];
        
        [TableView reloadData];
        
        TableView.frame=CGRectMake(0, 0, kDeviceWidth, TableView.contentSize.height);
        
        MoneyView.frame=CGRectMake(0, CGRectGetMaxY(TableView.frame), kDeviceWidth, 40);
        
        //    NSLog(@"%f",TableView.contentSize.height);
        //
        MainSc.contentSize=CGSizeMake(0, CGRectGetMaxY(TableView.frame)+40);
        
    }];
    UIAlertAction *no=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        
    }];
    
    [alert addAction:ok];
    [alert addAction:no];
    
    [self presentViewController:alert animated:YES completion:nil];
    
    
    
}
@end





仿钉钉报销demo.zip (401.12 KB, 下载次数: 571)
1659 3 0
联系我们
首页/微信公众账号投稿

帖子代码编辑/版权问题

QQ:435399051,742864542


代码贡献英雄榜
用户名 下载数
通过邮件订阅最新 Code4App 信息
上一条 /4 下一条
联系我们
关闭
合作电话:
13802416937
Email:
435399051@qq.com
商务市场合作/投稿
问题反馈及帮助
联系我们

广告投放| Github|申请友链|手机版|Code4App ( 粤ICP备15117877号-1 )

快速回复 返回顶部 返回列表