美文网首页
tableView 简易通讯录的添加

tableView 简易通讯录的添加

作者: yi叶知秋 | 来源:发表于2016-06-29 10:51 被阅读84次
效果图
#import "ViewController.h"
#import "People.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>
{
    UITableView *_tableView;
    NSMutableArray *_dataArray;
}
@end
@implementation ViewController

-(void)dealloc
{
    [_dataArray release];
    [_tableView release];
[super dealloc];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, 320, 480-64) style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    _dataArray = [[NSMutableArray alloc] init];
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemAdd) target:self action:@selector(addData)];
    self.navigationItem.rightBarButtonItem = rightBtn;
    [rightBtn release];
}
#pragma mark -- 添加按钮点击方法
-(void)addData
{
    UIAlertView *alertVIew = [[UIAlertView alloc] initWithTitle:@"添加联系人" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
    //设置警告框的样式
    //UIAlertViewStyleLoginAndPasswordInput  带有两个输入框
    alertVIew.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
    //textFieldAtIndex 根据索引从警示框上找输入框,索引从0 1
    UITextField *nameTF = [alertVIew textFieldAtIndex:0];
    UITextField *phoneTF = [alertVIew textFieldAtIndex:1];
    //占位符
    nameTF.placeholder = @"姓名";
    phoneTF.placeholder = @"电话";
    //安全输入
    phoneTF.secureTextEntry = NO;
    //UIKeyboardTypeNumberPad 数字键盘
    phoneTF.keyboardType = UIKeyboardTypeNumberPad;
    [alertVIew show];
    [alertVIew release];
}

#pragma mark -- alterView协议方法
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        NSLog(@"点击取消");
        return;
    }
    //获取输入框
    UITextField *nameTF = [alertView textFieldAtIndex:0];
    UITextField *phoneTF = [alertView textFieldAtIndex:1];
    //把姓名和电话存到一个数据类里面
    People *p = [[People alloc] init];
    //获取姓名
    p.name = nameTF.text;
    //获取电话
    p.phone = phoneTF.text.intValue;
    [_dataArray addObject:p];
    [p release];
    /*
     表的刷新
     1.reloadData
     2.表插入数据的方法 insertRowsAtIndexPaths:withRowAnimation 当在表中插入数据的时候使用
     3.表删除数据的方法 deleteRowsAtIndexPaths:withRowAnimation 当从表中删除数据的时候使用
     4.cell的移动不需写代码手动刷新表
     5.刷新指定的区(还可以刷新指定连续的区 还可以刷新指定不连续的区 ,所刷新的去由索引的集合(NSIndexSet *)) reloadSections:withRowAnimation:
     */
    //刷新表的指定区域
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_dataArray.count-1 inSection:0];
    [_tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationFade)];

}
#pragma mark -- 表的协议方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"cellID"];
    }
    //显示姓名和电话
    People *p = [_dataArray objectAtIndex:indexPath.row];
    cell.textLabel.text = p.name;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%d",p.phone];
    return cell;
}
@end

相关文章

网友评论

      本文标题:tableView 简易通讯录的添加

      本文链接:https://www.haomeiwen.com/subject/clfxjttx.html