iOS 动态设置cell

    在开发中有时候会遇到在初始化tableView的时候没有数据,但是在进行下一步操作或者自动请求后又需要往tableView中添加cell的需求。实现的基本思路是:定义一个全局的可变数组,在初始化的适合数组为长度0,请求数据后,将数据放入数组,然后再刷新表格。

  1. 定义全局的可变数组:

@property (nonatomic, strong) NSMutableArray *familyNumberArray;

数组方法:

-(NSMutableArray *)familyNumberArray
{
    if (_familyNumberArray == nil) {
        _familyNumberArray = [NSMutableArray array];
    }
    return _familyNumberArray;
}

2.将数据放入数组:

NSArray *array = [responseObj componentsSeparatedByString:@"#"];
            for (int i = 0; i < [array count]; i++) {
                if (![[array objectAtIndex:i] isEqualToString:@""]) {
                    //                NSLog(@"string:%@", [array objectAtIndex:i]);
                    [self.familyNumberArray addObject:[array objectAtIndex:i]];
                }
            }

3.刷新表格:

// 刷新表格
[self.tableView reloadData];



对整个功能来说,上面的描述并不直观,下面放上完整代码:

#import "HHBoundFamilyNumberViewController.h"
#import "HHHttpTool.h"
#import "HHDevice.h"
#import "MBProgressHUD+Extend.h"

@interface HHBoundFamilyNumberViewController () <UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITextField *phoneNumber;
@property (weak, nonatomic) IBOutlet UIButton *bindingBut;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *familyNumberArray;

@end

@implementation HHBoundFamilyNumberViewController

-(NSMutableArray *)familyNumberArray
{
    if (_familyNumberArray == nil) {
        _familyNumberArray = [NSMutableArray array];
    }
    return _familyNumberArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    HHLog(@"Bound   %@",self.device.IMSI);
    
    [_phoneNumber addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
    
    // 设置tableView数据
    self.tableView.dataSource = self;
    
    self.tableView.delegate = self;
    
    // 获取亲情号码列表
    [self getFamilyNumberList];
}

- (void)textChange {
    // 判断两个文本框的内容
    _bindingBut.enabled = _phoneNumber.text.length;
}

// 获取亲情号码列表
- (void)getFamilyNumberList {
    // 1.封装请求参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"IMSI"] = self.device.IMSI;
    
    NSString *url = [ServiceInfo stringByAppendingString:@"Application/getFamilyNumber"];
    
    HHLog(@"%@",url);
    
    // 2.发送请求
    [HHHttpTool get:url parame:params success:^(id responseObj) {
        HHLog(@"-----------%@",responseObj);
        if (![responseObj isEqualToString:@"NO"]) {
            NSArray *array = [responseObj componentsSeparatedByString:@"#"];
            for (int i = 0; i < [array count]; i++) {
                if (![[array objectAtIndex:i] isEqualToString:@""]) {
                    //                NSLog(@"string:%@", [array objectAtIndex:i]);
                    [self.familyNumberArray addObject:[array objectAtIndex:i]];
                }
            }
        }
        
        // 刷新表格
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        HHLog(@"error  %@",error);
    }];
}

// 绑定亲情号码
- (IBAction)bindingFamilyNumber:(id)sender {
    
    if (self.phoneNumber.text.length < 3) {
        [MBProgressHUD showError:@"请输入正确的亲情号码"];
        return;
    }
    
    // 1.封装请求参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"IMSI"] = self.device.IMSI;
    params[@"familyNumber"] = self.phoneNumber.text;
    
    NSString *url = [ServiceInfo stringByAppendingString:@"Application/familyNumber"];
    
    HHLog(@"%@",url);
    
    // 2.发送请求
    [HHHttpTool get:url parame:params success:^(id responseObj) {
        HHLog(@"-----------%@",responseObj);
        if ([responseObj isEqualToString:@"OK"]) {
            [MBProgressHUD showSuccess:@"绑定成功"];
            
            // 1.添加数据
            [self.familyNumberArray addObject:self.phoneNumber.text];
            // 2.刷新表格
            [self.tableView reloadData];
        }else {
            [MBProgressHUD showError:@"绑定失败"];
        }
    } failure:^(NSError *error) {
        HHLog(@"error  %@",error);
    }];
}

// 返回每一组有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.familyNumberArray.count;
}

// 返回当前行显示的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"familyNumber";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if(cell == nil){
        cell = [[UITableViewCell alloc] init];
    }

    // 给cell的控件赋值
    NSString *number = [@"NO." stringByAppendingString:[NSString stringWithFormat:@"%ld:", (indexPath.row + 1)]];
    cell.textLabel.text = [number stringByAppendingString:self.familyNumberArray[indexPath.row]];
    
    return cell;
}

/**
 *  实现滑动删除
 */
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1.封装请求参数
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"IMSI"] = self.device.IMSI;
    params[@"familyNumber"] = self.familyNumberArray[indexPath.row];
    
    NSString *url = [ServiceInfo stringByAppendingString:@"Application/relieveBinding"];
    
    HHLog(@"%@",url);
    
    // 2.发送请求
    [HHHttpTool get:url parame:params success:^(id responseObj) {
        HHLog(@"%@",responseObj);
        if ([responseObj isEqualToString:@"OK"]) {
            [MBProgressHUD showSuccess:@"删除成功"];
        }else {
            [MBProgressHUD showError:@"删除失败"];
        }
        
    } failure:^(NSError *error) {
        HHLog(@"error  %@",error);
    }];
    
    // 1.删除模型数据
    [self.familyNumberArray removeObjectAtIndex:indexPath.row];
    // 2.刷新表格
    //    [self.tableView reloadData];
    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
}

@end


你可能感兴趣的:(iOS 动态设置cell)