11

का उपयोग कर तालिका दृश्य कक्ष से एक विस्तार दृश्य को धक्का देना मेरे पास व्यू कंट्रोलर के अंदर एक टेबल व्यू है। मैं टेबल दृश्य के अंदर अपनी सारी जानकारी पॉप्युलेट कर सकता हूं। हालांकि मैं विस्तार विचारों को स्थापित करने के लिए थोड़ा सा खो गया हूं। मेरा मानना ​​है कि प्रत्येक टेबल सेल को प्रत्येक विवरण दृश्य में एक सेगू की आवश्यकता होती है लेकिन पूरी तरह से सुनिश्चित नहीं होती है।एक्सकोड स्टोरीबोर्ड

मेरा कोड यहां है। तालिका दृश्य से सीगू को विस्तार दृश्यों में पूरा करने के लिए मुझे क्या याद आ रही है? कोड:

.h 

@interface DetailViewController : UIViewController <UITableViewDelegate,UITableViewDataSource> 
{ 
    IBOutlet UITableView *myTable; 
    NSMutableArray *contentArray; 
} 

@property (strong, nonatomic) IBOutlet UITableView *myTable; 

.m 


- (void)viewDidLoad 
{ 
    contentArray = [[NSMutableArray alloc]init]; 
    [contentArray addObject:@"Espresso"]; 
    [contentArray addObject:@"Latte"]; 
    [contentArray addObject:@"Capicino"]; 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
} 

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [contentArray count]; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath 
{ 

    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

    if([[contentArray objectAtIndex:indexPath.row]isEqualToString:@"EspressoViewController"]) 
    { 
     EspressoViewController *espresso = [[EspressoViewController alloc]initWithNibName:@"EspressoViewController" bundle:nil]; 
     [self.navigationController pushViewController:espresso animated:YES]; 
    } 
    else if ([[contentArray objectAtIndex:indexPath.row] isEqualToString:@"Latte"]) 
    { 
     LatteViewController *latte = [[LatteViewController alloc] initWithNibName:@"Latte" bundle:nil]; 
     [self.navigationController pushViewController:latte animated:YES]; 
    } 

} 

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath 
{ 
    [self tableView:tableView didSelectRowAtIndexPath:indexPath]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"CellIdentifier"]; 
    } 

    NSString *cellValue = [contentArray objectAtIndex:indexPath.row]; 
    cell.textLabel.text = cellValue; 

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    cell.textLabel.font = [UIFont systemFontOfSize:16]; 
    cell.detailTextLabel.text = @"Hot and ready"; 

    UIImage *image = [UIImage imageNamed:@"coffeeButton.png"]; 
    cell.imageView.image = image; 

    cell.textLabel.text = [contentArray objectAtIndex:indexPath.row]; 
    return cell; 
} 

उत्तर

17

मैं आप इस एक छोटे से भी जटिल बना दिया है। चिंता मत करो, मैं वही काम करता हूं।

सबसे पहले, tableView:accessoryButtonTappedForRowAtIndexPath: के भीतर से tableView:didSelectRowAtIndexPath: भेजकर दो विधियों के बीच कोई अंतर नहीं है। सेल टैप करना, या इसका एक्सेसरी बटन एक ही क्रिया करता है। यदि आपको सेल को टैप करने से अलग कार्रवाई करने के लिए एक्सेसरी बटन की आवश्यकता नहीं है, तो इसे हटा दें।

दूसरा, यदि आप स्टोरीबोर्ड का उपयोग कर रहे हैं, तो आपको अपने व्यू कंट्रोलर के लिए आवंटित/initWithNib की आवश्यकता नहीं है। इसके बजाय, एक segue का उपयोग करें। बाहर एक UITableViewController

  1. खींचें: आप स्टोरीबोर्ड के माध्यम से ऐसा करते हैं तो आप भी प्रोग्राम के रूप में अपने navigationController

    पर viewControllers पुश करने के लिए

    अपने स्टोरीबोर्ड पहले बिल्ड जरूरत नहीं है। सुनिश्चित करें कि आपने UITableViewController की कक्षा को सेट किया है जिसे आपने दाईं ओर इंस्पेक्टर फलक का उपयोग करके अपने "DetailViewController" पर खींच लिया है।

  2. फिर इस नियंत्रक का चयन करें और मेनू का उपयोग कर "संपादक ->एम्बेड में ->नेविगेशन नियंत्रक" चुनें।
  3. अगला, तीन सामान्य UIViewControllers को खींचें। एक के वर्ग को "LatteViewController" पर सेट करें, दूसरा "एस्प्रेसो व्यू कंट्रोलर", और एक तिहाई "CapicinoViewController" (इंस्पेक्टर का उपयोग करके)।
  4. इन दृश्यों में से प्रत्येक को UITableViewController से नियंत्रण + खींचें नियंत्रक और PUSH चुनें।
  5. अपने UITableViewController और इनमें से प्रत्येक दृश्य नियंत्रक के बीच तीर पर स्थित छोटे सर्कल पर क्लिक करें। इंस्पेक्टर (दाईं ओर) में, प्रत्येक segue को पहचानकर्ता फ़ील्ड में एक अद्वितीय नाम दें। आपको अपने कोड के लिए यह नाम याद रखना होगा। मैं उन्हें "एस्प्रेसोसेगु", "लेटटेसेगु" और "कैपिसीनोसेगु" नाम दूंगा। आप देखेंगे कि नीचे दिए गए कोड में क्यों।

    - (void)tableView:(UITableView *)tableView 
    didSelectRowAtIndexPath:(NSIndexPath*)indexPath { 
    
    //Build a segue string based on the selected cell 
    NSString *segueString = [NSString stringWithFormat:@"%@Segue", 
             [contentArray objectAtIndex:indexPath.row]]; 
    //Since contentArray is an array of strings, we can use it to build a unique 
    //identifier for each segue. 
    
    //Perform a segue. 
    [self performSegueWithIdentifier:segueString 
              sender:[contentArray objectAtIndex:indexPath.row]]; 
    } 
    

    तुम कैसे लागू बाकी आप पर निर्भर है:

फिर अपने UITableViewController में निम्न कोड डालें। आप अपने UITableViewController में prepareForSegue:sender: को कार्यान्वित करना चाहते हैं और फिर उस विधि का उपयोग segue.destinationViewController पर जानकारी भेज सकते हैं।

ध्यान दें कि मैंने आपकी सामग्री से स्ट्रिंग को पास किया है प्रेषक के रूप में प्रेषक के रूप में। आप जो भी चाहें पास कर सकते हैं। सेल की पहचान करने वाली स्ट्रिंग पास करने के लिए सबसे तार्किक जानकारी की तरह लगती है, लेकिन पसंद आपके ऊपर है।

ऊपर पोस्ट किया गया कोड आपके लिए नेविगेशन करना चाहिए।

संबंधित मुद्दे