iOS開(kāi)發(fā)關(guān)于通訊錄界面及刪除聯(lián)系人的實(shí)現(xiàn)(UI階段)

前面我寫(xiě)的文章中有關(guān)于通訊錄實(shí)現(xiàn)和添加聯(lián)系人的實(shí)現(xiàn),今天介紹通訊錄刪除聯(lián)系人的實(shí)現(xiàn).因?yàn)楹芏喑跏蓟姆椒ㄔ谇懊嫣砑勇?lián)系人的文章中都有介紹,本文就不在詳細(xì)說(shuō)明了!(刪除相比添加而言相對(duì)復(fù)雜,建議先看添加界面的實(shí)現(xiàn)文章).

在延展中定義數(shù)據(jù)源數(shù)組和tableView

@property(nonatomic, retain)NSMutableArray *dataSource;
@property(nonatomic, retain)UITableView *tableView;

在viewDidLoad中添加title和顏色:

- (void)viewDidLoad {
 [super viewDidLoad];

self.navigationItem.title = @"通訊錄";
self.view.backgroundColor = [UIColor whiteColor];
}

拖入已有的聯(lián)系人文件:

99DF1360-5861-473A-9A4B-2A2FD374911A.png

初始化tableView:

  self.tableView = [[UITableView alloc] 
 initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 
 self.view.frame.size.height - 64) 
style:UITableViewStylePlain];
#設(shè)置代理人 (這一步非常關(guān)鍵,千萬(wàn)不要遺漏)
self.tableView.delegate = self;
self.tableView.dataSource = self;

[self.view addSubview:_tableView];
[_tableView release];

設(shè)置頭視圖按鈕:

#這些都是比較簡(jiǎn)單的初始化方法,這里不多介紹
  UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
self.tableView.tableHeaderView = header;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 175, 40);
button.center = header.center;
[button setTitle:@"刪除" forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

button.backgroundColor = [UIColor blueColor];
[button addTarget:self action:@selector(didClickButton1:) forControlEvents:UIControlEventTouchUpInside];
[header addSubview:button];

[header release];

簽訂tableView的協(xié)議:

    @interface ViewController ()<UITableViewDelegate, 
UITableViewDataSource>

設(shè)置cell的高度:

//cell的高度
- (CGFloat)tableView:(UITableView *)tableView 
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}

設(shè)置可編輯的樣式(即刪除):

 - (UITableViewCellEditingStyle)tableView:(UITableView   
*)tableView editingStyleForRowAtIndexPath:(NSIndexPath 
*)indexPath
{
 return UITableViewCellEditingStyleDelete;
}

因?yàn)樵谝粋€(gè)APP中不只一個(gè)編輯樣式,所以要加判斷看是否是刪除操作:

- (void)tableView:(UITableView *)tableView  
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
    [self.dataSource removeObjectAtIndex:indexPath.row];
    [tableView reloadData];
}
}

tableView必須實(shí)現(xiàn)的兩個(gè)方法:

cell的行數(shù):
  - (NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section
{
return self.dataSource.count;
}
內(nèi)容:
- (UITableViewCell *)tableView:(UITableView *)tableView 
  cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
cell.textLabel.text = [[self.dataSource objectAtIndex:indexPath.row] valueForKey:@"name"];
//判斷右側(cè)小圖標(biāo)是否正在被選中:
if (![self.deleteIndexPathArray containsObject: indexPath]) {
    cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
return cell;
}

為按鈕添加點(diǎn)擊方法:

 - (void)didClickButton1:(UIButton *)button
{
if (self.deleteIndexPathArray == nil) {
    NSLog(@"數(shù)組為空");
    
 }
else
{
    //將準(zhǔn)備刪除的數(shù)組放到一個(gè)臨時(shí)數(shù)組中:
    NSMutableArray *tempArray = [NSMutableArray array];
    //遍歷deleteIndexPathArray
    for (NSIndexPath *indexPath in self.deleteIndexPathArray) {
        NSDictionary *dic = [self.dataSource objectAtIndex:indexPath.row];
        [tempArray addObject:dic];
    }
    [self.dataSource removeObjectsInArray:tempArray];
    [self.deleteIndexPathArray removeAllObjects];
    
}
[self.tableView reloadData];

}

cell的點(diǎn)擊方法:

- (void)tableView:(UITableView *)tableView   
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

//判斷數(shù)組里是否包含該indexPash
if ([self.deleteIndexPathArray containsObject:indexPath]) {
    [self.deleteIndexPathArray removeObject:indexPath];
    //取消點(diǎn)擊狀態(tài):
    cell.accessoryType = UITableViewCellAccessoryNone;

}
else
{
    [self.deleteIndexPathArray addObject:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;

}

}

編輯完成 刪除點(diǎn)擊的cell

    #pragma mark -- 編輯完成:(處理數(shù)據(jù),更新視圖)
- (void)tableView:(UITableView *)tableView 
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
forRowAtIndexPath:(NSIndexPath *)indexPath
{
//判斷編輯的樣式:
if (editingStyle == UITableViewCellEditingStyleDelete) {
    //刪除數(shù)組對(duì)應(yīng)的元素:
    [self.dataSource removeObjectAtIndex:indexPath.row];
    //刷新tableView:
    [tableView reloadData];
    
    }
}

結(jié)合前面幾天的添加聯(lián)系人的實(shí)現(xiàn)能更好的理解這部分內(nèi)容.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容