2012-02-10 9 views
10

जो आप नीचे देखते हैं वह Tweetbot iPhone application का स्क्रीन शॉट है। मैं जो पूछना चाहता हूं वह यह है कि यदि आप जानते हैं कि टैपबॉट्स लोगों ने अपनी तालिका में क्या हासिल किया है, तो आप यह प्राप्त कर सकते हैं: आप पूर्व तालिका के सेल पर टैप करें और यह क्रियाओं का एक सेट दिखाता है, फिर से टैप करें और गायब हो जाता है।कस्टमाइज्ड टेबल व्यू à la Tweetbot

enter image description here

यह एक बहुत अच्छा लग रही कार्यान्वयन है और मुझे लगता है मैं कैसे कर सकते हैं पर आपकी राय जानना अच्छा लगेगा दोनों अभ्यास के लिए और इसलिए भी क्योंकि मैं एक भविष्य परियोजना में इस का एक संस्करण का उपयोग करने के प्यार होता है कि।

कोई विचार?

+0

मुझे लगता है कि 'tableView: didSelectRowAtIndexPath' विधि में वह चयनित इंडेक्सपैथ पर पंक्ति को फिर से लोड करता है। और कस्टम सेल में टैप पर वह बड़ा या ऐसा कुछ बनाता है। –

उत्तर

24

जब उपयोगकर्ता सेल का चयन करता है तो चयनित सेल के इंडेक्स पथ को सहेजता है और नीचे एक पंक्ति जोड़ता है। यदि उपयोगकर्ता एक ही बिक्री का चयन करता है तो नीचे की पंक्ति को छुपाएं। यदि उपयोगकर्ता किसी अन्य सेल का चयन वर्तमान में चयनित सेल को छुपाता है और नए चुने हुए सेल के नीचे एक नया सेल दिखाता है।

@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

अच्छी सलाह, धन्यवाद। – Francesco

+0

अच्छा और बहुत उपयोगी कोड के लिए धन्यवाद! – Lindemann