類似qq好友列表的實現

今天在工作的過程中,有一個頁面的實現類似于qq好友列表。經過上網查資料,自己進行了簡單的實現。

數據源(為plist文件):
<array>
 <dict>
  <key>groupname</key>
  <string>初中同學</string>
  <key>list</key>
  <array>
   <dict>
    <key>name</key>
    <string>初中同學1</string>
    <key>isonline</key>
    <true/>
    <key>imagename</key>
    <string>head01</string>
   </dict>
   <dict>
    <key>name</key>
    <string>初中同學2</string>
    <key>isonline</key>
    <true/>
    <key>imagename</key>
    <string>head02</string>
   </dict>
   <dict>
    <key>name</key>
    <string>初中同學3</string>
    <key>isonline</key>
    <false/>
    <key>imagename</key>
    <string>head03</string>
   </dict>
  </array>
 </dict>
 <dict>
  <key>groupname</key>
  <string>高中同學</string>
  <key>list</key>
  <array>
   <dict>
    <key>name</key>
    <string>高中同學1</string>
    <key>isonline</key>
    <true/>
    <key>imagename</key>
    <string>head08</string>
   </dict>
   <dict>
    <key>name</key>
    <string>高中同學2</string>
    <key>isonline</key>
    <true/>
    <key>imagename</key>
    <string>head05</string>
   </dict>
   <dict>
    <key>name</key>
    <string>初中同學3</string>
    <key>isonline</key>
    <false/>
    <key>imagename</key>
    <string>head07</string>
   </dict>
  </array>
 </dict>
 <dict>
  <key>groupname</key>
  <string>大學同學</string>
  <key>list</key>
  <array>
   <dict>
    <key>name</key>
    <string>大學同學1</string>
    <key>isonline</key>
    <true/>
    <key>imagename</key>
    <string>head04</string>
   </dict>
   <dict>
    <key>name</key>
    <string>大學同學2</string>
    <key>isonline</key>
    <false/>
    <key>imagename</key>
    <string>head07</string>
   </dict>
  </array>
 </dict>
</array>

通過代碼,將上面的數據通過代碼進行調整,可以實現類似于qq好友列表的樣式:標題分別是初中同學高中同學大學同學
??整體思路是這樣的,整個頁面是一個分組樣式的tableView,這些標題可以放到headerView上,通過點擊headerView來判斷該分區是開還是收。不多說了,直接上代碼。

@interface MyDevicesTableViewController ()

@property (nonatomic, retain)NSArray * dataList;            // 全部的好友信息
@property (nonatomic, retain)NSMutableArray * groupNames;   // 分組標題
@property (nonatomic, retain)NSMutableDictionary * headers; // 存放headerView的狀態的字典
@property (nonatomic, retain)NSString * isOpen;             // headerView的狀態

@end
/**
 *  獲取數據
 */
- (void)loadData
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"friends" ofType:@"plist"];
    self.dataList = [NSArray arrayWithContentsOfFile:path];
    self.groupNames = [NSMutableArray arrayWithCapacity:self.dataList.count];
    
    for (NSInteger i = 0; i < self.dataList.count; i++)
    {
        NSDictionary *dict = self.dataList[i];
        [self.headers setObject:@"NO" forKey:[NSString stringWithFormat:@"%d",i]];
        [self.groupNames addObject:dict[@"groupname"]];
    }
//    NSLog(@"_dataList = %@", self.dataList);
//    NSLog(@"_headers = %@", self.headers);
//    NSLog(@"_groupNames = %@", self.groupNames);
}
實現tableView的代理方法:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return self.groupNames.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    self.isOpen = [self.headers objectForKey:[NSString stringWithFormat:@"%d", section]];
    NSArray *array = self.dataList[section][@"list"];
    if ([self.isOpen isEqualToString:@"YES"]) {
        
        return array.count;
    } else {
        
        return 0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 45;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.tag = section + 100;
    button.bounds = CGRectMake(0, 0, self.view.frame.size.width, 45);
    button.backgroundColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.8 alpha:1.0];
    button.titleLabel.font = [UIFont systemFontOfSize:16.0f];
    NSString *title = self.groupNames[section];
    [button setTitle:title forState:UIControlStateNormal];
    [button addTarget:self action:@selector(expandFriends:) forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *TableSampleIdentifier = @"myDevices";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableSampleIdentifier];
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableSampleIdentifier];
    }
    
    NSArray *array = self.dataList[indexPath.section][@"list"];
    cell.textLabel.text = [array objectAtIndex:indexPath.row][@"name"];
    
    return cell;
}

- (void)expandFriends:(UIButton *)header
{
    
    NSInteger section = header.tag - 100;
    
    self.isOpen = [self.headers objectForKey:[NSString stringWithFormat:@"%d", section]];
    if ([self.isOpen isEqualToString:@"YES"]) {
        
        self.isOpen = @"NO";
    } else {
        
        self.isOpen = @"YES";
    }
    
    [self.headers setObject:self.isOpen forKey:[NSString stringWithFormat:@"%d", section]];
    [self.tableView reloadData];
}

以上就是全部的代碼,希望能夠對你起到幫助。當然,這只是進行了簡單實現,沒有進一步的完善。比如:標題現在沒有圖片,并且文字居中,和qq好友列表不同。由于時間問題,我沒有實現。希望大神斧正。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,548評論 25 708
  • 約翰也救了我,但多年以后,當我站在亞拉巴馬的滂沱大雨中尋找并不存在于立交橋下陰影中的那個人時,當我站在四處散落的行...
    李澤賢閱讀 887評論 0 3
  • 簽名板介紹 在最近的一個項目中,最后的一個功能是實現一個簽名板供客戶簽名使用。這需要用到canvas來實現。 我將...
    LeoMelody閱讀 8,321評論 5 9
  • 《接吻》 喜歡就要接吻而愛就是把你吃掉 《戒》 我想戒掉你或者讓我們在一起 《忘記》 那年我們說好了要在一起結局是...
    何鯨洛閱讀 248評論 0 0
  • 命運不可盡知,君子藏器于身,待時而動。
    碼一閱讀 196評論 0 0