2012-02-10 13 views
10

Hình bạn thấy bên dưới là ảnh chụp màn hình Tweetbot iPhone application. Những gì tôi muốn hỏi là nếu bạn biết một cách tôi có thể đạt được những gì Tapbots guys đạt được trong tableView của họ: bạn gõ vào một tế bào của tableView trước đây và nó cho thấy một tập hợp các hành động, bấm một lần nữa và nó biến mất.Bảng tùy chỉnhXem à la Tweetbot

enter image description here

Đó là một thực hiện thực sự mát mẻ tìm kiếm và tôi rất thích biết ý kiến ​​của bạn về làm thế nào tôi có thể làm điều đó cho cả thực hành và cũng bởi vì tôi rất thích sử dụng một biến thể của điều này trong một dự án trong tương lai.

Bất kỳ ý tưởng nào?

+0

Tôi nghĩ trong phương thức 'tableView: didSelectRowAtIndexPath', anh ta chỉ cần tải lại hàng tại đường dẫn chỉ mục đã chọn. Và trên vòi trong tế bào tùy chỉnh, ông làm cho lớn hơn hoặc một cái gì đó như thế. –

Trả lời

24

Khi người dùng chọn ô lưu đường dẫn chỉ mục của ô đã chọn và thêm hàng bên dưới ô đó. Nếu người dùng chọn cùng một hàng, hãy ẩn hàng bên dưới nó. Nếu người dùng chọn một ô khác ẩn ô hiện đang được chọn và hiển thị ô mới bên dưới ô được chọn mới.

@interface RCTableViewController() 

@property (nonatomic, strong) NSMutableArray *tableViewData; 
@property (nonatomic, strong) NSIndexPath *selectedIndexPath; 

@end 

@implementation RCTableViewController 

@synthesize tableViewData = _tableViewData; 
@synthesize selectedIndexPath = _selectedIndexPath; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.tableViewData = [NSMutableArray arrayWithObjects: 
       @"Cell 0", 
       @"Cell 1", 
       @"Cell 2", 
       @"Cell 3", 
       @"Cell 4", 
       @"Cell 5", 
       @"Cell 6", 
       @"Cell 7", 
       @"Cell 8", 
       nil]; 
    self.selectedIndexPath = nil; 
    [self.tableView reloadData]; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return self.tableViewData.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell; 

    if (indexPath == self.selectedIndexPath) { 
     static NSString *DropDownCellIdentifier = @"DropDownCell"; 

     cell = [tableView dequeueReusableCellWithIdentifier:DropDownCellIdentifier]; 
     if (cell == nil) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DropDownCellIdentifier]; 
     } 

     cell.textLabel.text = @"Drop Down Cell"; 

    } else { 
     static NSString *DataCellIdentifier = @"DataCell"; 

     cell = [tableView dequeueReusableCellWithIdentifier:DataCellIdentifier]; 
     if (cell == nil) { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DataCellIdentifier]; 
     } 

     cell.textLabel.text = [self.tableViewData objectAtIndex:indexPath.row]; 
    } 

    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSIndexPath *dropDownCellIndexPath = [NSIndexPath indexPathForRow:indexPath.row + 1 
                  inSection:indexPath.section]; 

    if (!self.selectedIndexPath) { 
     // Show Drop Down Cell   
     [self.tableViewData insertObject:@"Drop Down Cell" atIndex:dropDownCellIndexPath.row]; 

     [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:dropDownCellIndexPath] 
         withRowAnimation:UITableViewRowAnimationTop];   

     self.selectedIndexPath = indexPath;  
    } else { 
     NSIndexPath *currentdropDownCellIndexPath = [NSIndexPath indexPathForRow:self.selectedIndexPath.row + 1 
                   inSection:self.selectedIndexPath.section]; 

     if (indexPath.row == self.selectedIndexPath.row) { 
      // Hide Drop Down Cell 
      [self.tableViewData removeObjectAtIndex:currentdropDownCellIndexPath.row]; 

      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:currentdropDownCellIndexPath] 
          withRowAnimation:UITableViewRowAnimationTop]; 

      self.selectedIndexPath = nil; 
     } else if (indexPath.row == currentdropDownCellIndexPath.row) { 
      // Dropdown Cell Selected - No Action 
      return; 
     } else { 
      // Switch Dropdown Cell Location  
      [tableView beginUpdates]; 
      // Hide Dropdown Cell 
      [self.tableViewData removeObjectAtIndex:currentdropDownCellIndexPath.row]; 

      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:currentdropDownCellIndexPath] 
          withRowAnimation:UITableViewRowAnimationAutomatic]; 

      // Show Dropdown Cell    
      NSInteger dropDownCellRow = indexPath.row + ((indexPath.row >= currentdropDownCellIndexPath.row) ? 0 : 1); 
      dropDownCellIndexPath = [NSIndexPath indexPathForRow:dropDownCellRow 
                 inSection:indexPath.section]; 


      [self.tableViewData insertObject:@"Drop Down Cell" atIndex:dropDownCellIndexPath.row]; 

      [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:dropDownCellIndexPath] 
          withRowAnimation:UITableViewRowAnimationAutomatic];   

      self.selectedIndexPath = [NSIndexPath indexPathForRow:dropDownCellIndexPath.row - 1 
                 inSection:dropDownCellIndexPath.section]; 
      [tableView endUpdates];        
     }   
    } 
} 

@end 
+0

lời khuyên tốt đẹp, cảm ơn bạn. – Francesco

+0

Cảm ơn bạn vì mã đẹp và rất hữu ích! – Lindemann