天天看點

IOS詳解TableView——靜态表格使用以及控制器間通訊

一個Demo 通過使用靜态表格來完成個人資料增加編輯以及搜尋。不過通常我們會使用更靈活的Group風格表視圖來完成。

上篇文章簡單的介紹了一下搜尋框的使用。這裡給其加入資料來說明。

先看下效果

IOS詳解TableView——靜态表格使用以及控制器間通訊
IOS詳解TableView——靜态表格使用以及控制器間通訊
IOS詳解TableView——靜态表格使用以及控制器間通訊
IOS詳解TableView——靜态表格使用以及控制器間通訊

主要涉及到三個頁面,清單頁面,顯示資訊頁面,以及添加/編輯界面。

主要解決的就是視圖控制器間的資料通訊問題,以及在搜尋表格點選後更改資料同樣能進行正确地傳遞。這裡的資料通訊采用了比較常用的代理設計模式。

storyboard

IOS詳解TableView——靜态表格使用以及控制器間通訊

個人資訊儲存在一個模型類Person中

@interface Person : NSObject <NSCoding>

@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) UIImage *headerImage;
@property (strong, nonatomic) NSString *qq;
@property (strong, nonatomic) NSString *sex;
@property (strong, nonatomic) NSString *birthday;
@property (strong, nonatomic) NSString *signature;

@end
           

遵守了NSCoding 可以對其資訊進行歸檔解檔。

為了能正确顯示搜尋框的内容,需要對表視圖的資料源和代理方法對tableView進行判斷,比如表格視圖顯示的内容

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * const CellIdentifier = @"PersonCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    Person *p;
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        p = _resultList[indexPath.row];
    }
    else
    {
        p = _personList[indexPath.row];
    }

    cell.textLabel.text = p.name;
    cell.detailTextLabel.text = p.signature;
    cell.imageView.image = p.headerImage;
    
    return cell;
}
           

清單頁面上點選加号與點選Cell進入不同的頁面

IOS詳解TableView——靜态表格使用以及控制器間通訊

是以要判斷并給Segue設定辨別符

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"AddSegue"])
    {
        EditViewController *evc = (EditViewController *)segue.destinationViewController;
        evc.editDelegate = self;
    }
    if ([segue.identifier isEqualToString:@"InfoSegue"])
    {
        _infoVC = (InfoViewController *)segue.destinationViewController;
        _infoVC.infoDelegate = self;
    }
}
           

注意到這裡把兩個視圖控制器的Delegate都設給了自己,主要是為了在編輯和增加過後,能把資料模型的資訊發送給首頁的試圖控制器,讓其重新整理資料。

來看下兩個協定

@protocol EditViewControllerDelegate <NSObject>

@optional
- (void)sendAddPerson:(Person *)person;
- (void)sendEditPerson:(Person *)ePerson;

@end
           
@protocol InfoViewControllerDelegate <NSObject>

@optional
- (void)refreshPersonData:(Person *)personData;

@end
           

主視圖控制器對代理方法的實作

- (void)sendAddPerson:(Person *)person
{
    if (!_personList)
    {
        _personList = [NSMutableArray array];
    }
    
    [_personList addObject:person];
    [self.tableView reloadData];
    
    NSString *path = [self pathForPersonList];
    [NSKeyedArchiver archiveRootObject:_personList toFile:path];
}

- (void)refreshPersonData:(Person *)personData
{
    [_personList removeObjectAtIndex:_personIndex];
    [_personList insertObject:personData atIndex:_personIndex];
    [_resultList removeObjectAtIndex:_resultIndex];
    [_resultList insertObject:personData atIndex:_resultIndex];
    
    [self.searchDisplayController.searchResultsTableView reloadData];
    [self.tableView reloadData];
    
    NSString *path = [self pathForPersonList];
    [NSKeyedArchiver archiveRootObject:_personList toFile:path];
}
           

然後在相應地試圖控制器中使用delegate在編輯或添加完成是,将資料發送過來就可以了。

還有就是注意由于在首頁有兩個tableview,則顯示的資料和編輯,響應點選方法都要設定不同的資料清單進行管理。

下面是響應點選的方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Person *person;
    if (tableView == self.tableView)
    {
        person = _personList[indexPath.row];
        _infoVC.person = person;
        _personIndex = indexPath.row;
    }
    else
    {
        [self performSegueWithIdentifier:@"InfoSegue" sender:nil];
        person = _resultList[indexPath.row];
        _infoVC.person = person;
        _personIndex = [_personList indexOfObject:person];
        _resultIndex = indexPath.row;
    }
}
           

這裡在點選搜尋顯示控制器上的tableview時,需要去perform顯示個人資訊的場景,并對其資料資訊進行儲存,以便修改後對搜尋資料清單的資訊也進行更新。

功能完成了,不過在實作一些方法時可以根據個人習慣對代碼進行重構。把源碼貼出來

Demo源碼:點選打開連結

以上為本篇部落格全部内容,歡迎指正和交流。轉載注明出處~