天天看點

UI開發----簡單通訊錄的實作實作分組展⽰示學員通訊錄。 1、使⽤用tableView展⽰示學員通訊錄,根據聯系⼈人分組使⽤用分區顯⽰示聯系 ⼈人,分區header顯⽰示分組名(A~Z)。并提供分區索引。 2、使⽤用UITableViewCell展⽰示聯系⼈人頭像、姓名、聯系⽅方式。 3、選中聯系⼈人進⼊入詳情⻚頁⾯面,顯⽰示聯系⼈人資訊(頭像、姓名、性别、年齡、聯系⽅方式、愛好)。 4、定義Model類(Student)。使⽤用ContactList.plist提供資料源。

 // Created By 郭仔  2015年04月23日23:18:08

// ===================================

簡單通訊錄的實作:

// ===================================

實作分組展⽰示學員通訊錄。

1、使⽤用tableView展⽰示學員通訊錄,根據聯系⼈人分組使⽤用分區顯⽰示聯系 ⼈人,分區header顯⽰示分組名(A~Z)。并提供分區索引。

2、使⽤用UITableViewCell展⽰示聯系⼈人頭像、姓名、聯系⽅方式。

 3、選中聯系⼈人進⼊入詳情⻚頁⾯面,顯⽰示聯系⼈人資訊(頭像、姓名、性别、年

齡、聯系⽅方式、愛好)。 

4、定義Model類(Student)。使⽤用ContactList.plist提供資料源。

實作狀态(有些爛):

UI開發----簡單通訊錄的實作實作分組展⽰示學員通訊錄。 1、使⽤用tableView展⽰示學員通訊錄,根據聯系⼈人分組使⽤用分區顯⽰示聯系 ⼈人,分區header顯⽰示分組名(A~Z)。并提供分區索引。 2、使⽤用UITableViewCell展⽰示聯系⼈人頭像、姓名、聯系⽅方式。 3、選中聯系⼈人進⼊入詳情⻚頁⾯面,顯⽰示聯系⼈人資訊(頭像、姓名、性别、年齡、聯系⽅方式、愛好)。 4、定義Model類(Student)。使⽤用ContactList.plist提供資料源。

圖檔和名字弄的有些爛,主要是小功能都實作了。

主要是根據UITableView來實作。

資料在plist檔案中存放,把資料取出封裝到Student中:

NSDictionary * dic = [[NSDictionary alloc]initWithContentsOfFile:@"/Users/lanou3g/Desktop/Lesson/通訊錄練習/通訊錄練習/Contact List.plist"];
        NSMutableDictionary *contactDic = [NSMutableDictionary dictionary];
        self.contactDic = contactDic;
        for (NSString * key in dic) {
            NSArray * arr = [dic objectForKey:key];
            NSMutableArray * contactsArray = [NSMutableArray array ];
            for (NSDictionary * pDic in arr) {
                Person * p = [[Person alloc]init];
                p.name = [pDic objectForKey:@"name"];
                p.sex = [pDic objectForKey:@"sex"];
                p.photo = [pDic objectForKey:@"photo"];
                p.age = [[pDic objectForKey:@"age"] intValue];
                [contactsArray addObject:p];
                [p release];
            }
            [contactDic setObject:contactsArray forKey:key];

            
        }
        
           

一個分區中的行數:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString * key = [[self.contactDic allKeys] objectAtIndex:section];
    long count = [[self.contactDic objectForKey:key] count];
    return count;
}
           

通訊錄中的分區數:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[self.contactDic allKeys] count];
}
           

利用重用機制建立cell:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"GuoZai" ];
    if (!cell) {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"GuoZai"] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    NSArray * arr = getPersons(indexPath,self.contactDic);
    Person * per = [arr objectAtIndex:indexPath.row];
    cell.textLabel.text = per.name;
    cell.detailTextLabel.text = per.sex;
    cell.imageView.image = [UIImage imageNamed:per.photo];
    return cell;
}
           

我自己封裝的方法,取出字典中Student個數:

NSArray* getPersons(NSIndexPath *indexPath,NSMutableDictionary * dic)
{
    NSString * key = [[dic allKeys] objectAtIndex:indexPath.section];
    NSArray * arr = [dic objectForKey:key];
    return arr;
}
           

區标題:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [[self.contactDic allKeys] objectAtIndex:section];
}
           

編輯狀态:

- (void)rightBtnClick:(UIBarButtonItem *)btn
{
    [self.tableView setEditing:YES animated:YES];
}
           

可以編輯的行:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!indexPath.section && !indexPath.row) {
        return NO;
    }
    return YES;
}
           

編輯格式:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!indexPath.section && indexPath.row == 1) {
        return UITableViewCellEditingStyleDelete;
    }
    else
        return UITableViewCellEditingStyleInsert;
}
           

編輯完成:移動資料

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSMutableArray * arr = (NSMutableArray *)getPersons(indexPath, _contactDic);
        [arr removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
    if (editingStyle == UITableViewCellEditingStyleInsert) {
        Person * p = [[Person alloc]init];
        p.name = @"郭仔";
        p.age = 25;
        p.sex = @"男";
        NSMutableArray * arr = (NSMutableArray *)getPersons(indexPath, _contactDic);
        [arr insertObject:p atIndex:indexPath.row];
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
}
           

移動的三個步驟:

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    NSMutableArray * oldArr = (NSMutableArray *)getPersons(sourceIndexPath, self.contactDic);
    Person * p = [[oldArr objectAtIndex:sourceIndexPath.row] retain];
    [oldArr removeObjectAtIndex:sourceIndexPath.row];
   // [tableView deleteRowsAtIndexPaths:@[sourceIndexPath] withRowAnimation:UITableViewRowAnimationLeft];
    NSMutableArray * newArr = (NSMutableArray *)getPersons(destinationIndexPath, self.contactDic);
    [newArr insertObject:p atIndex:destinationIndexPath.row];
    [p release];
}
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
        return proposedDestinationIndexPath;
    }
    else
        return sourceIndexPath;
}
           

頁面跳轉,檢視詳情頁:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ThirdViewController * thirdVC = [[ThirdViewController alloc]init];
    NSMutableArray * arr = (NSMutableArray * )getPersons(indexPath, _contactDic);
    thirdVC.per = [arr objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:thirdVC animated:YES];
    [thirdVC release];
}
           

利用的是屬性傳值。

一些記憶體釋放的問題自己考慮把。