2012-02-24 19 views
12

पर प्रोग्रामेटिक रूप से कक्ष जोड़ना मैंने हाल ही में आईफोन के लिए प्रोग्रामिंग शुरू कर दी है और मैं एक ऐसा एप्लिकेशन बना रहा हूं जो डेटाबेस से जुड़ता है और पंक्ति नामों का एक सेट प्राप्त करता है और उन्हें प्रदर्शित करता है। चयनित होने पर, पंक्तियां पृष्ठभूमि रंग परिवर्तन यानी आप कई चयन कर सकते हैं और वे सभी अलग-अलग रंग होंगे। इसलिए मुझे एक्सएमएल को सर्वर से वापस कोई समस्या नहीं मिल रही है और मैंने कक्षों को प्रदर्शित करने के लिए UITableView बनाया है। हालांकि, मुझे नहीं पता कि तालिका में कक्षों को कैसे जोड़ना है। मैंने insertRowsAtIndexPaths पर एक नज़र डाली थी, लेकिन मुझे यकीन नहीं है कि इसका उपयोग कैसे किया जाए? जैसा कि मैं समझता हूं, insertRowsAtIndexPaths दो पैरामीटर लेता है:UITableView

एक एनएसएआरएआरई जिसमें सेल को किस पंक्ति में होना चाहिए और किस खंड में होना चाहिए। इसके साथ समस्या यह है कि मेरे ऐप में पंक्तियों की एक गतिशील संख्या होगी। अगर मैं नहीं जानता कि मुझे कितनी पंक्तियां होंगी तो मैं एनएसएआरएआरई बनाने के बारे में कैसे जाऊं? क्या मैं एनएसएमयूटेबलएरे का उपयोग कर सकता हूं?

यह दूसरा पैरामीटर एक एनीमेशन है - यह बहुत सरल है।

मेरे पास एक और मुद्दा है जहां मैं वास्तव में कोशिकाओं को बना सकता हूं? आप सेलव्यू को टेबलव्यू में कैसे पास करते हैं?

मैंने प्रलेखन पढ़ने की कोशिश की है लेकिन यह बहुत स्पष्ट प्रतीत नहीं होता है! दृश्य नियंत्रक की लोडव्यू विधि के अंदर इस समय मेरे पास कोड है:

//Before this I get the XML from the server so I am ready to populate 
//cells and add them to the table view 
NSArray *cells = [NSArray arrayWithObjects: 
        [NSIndexPath indexPathForRow:0 inSection:0], 
        [NSIndexPath indexPathForRow:1 inSection:0], 
        [NSIndexPath indexPathForRow:2 inSection:0], 
        [NSIndexPath indexPathForRow:3 inSection:0], 
        [NSIndexPath indexPathForRow:4 inSection:0], 
        [NSIndexPath indexPathForRow:5 inSection:0], 
        [NSIndexPath indexPathForRow:6 inSection:0], 
        [NSIndexPath indexPathForRow:7 inSection:0], 
        [NSIndexPath indexPathForRow:8 inSection:0], 
        [NSIndexPath indexPathForRow:9 inSection:0], 
        [NSIndexPath indexPathForRow:10 inSection:0], 
        [NSIndexPath indexPathForRow:11 inSection:0], 
        [NSIndexPath indexPathForRow:12 inSection:0], 
        nil]; 
[eventTypesTable beginUpdates]; 
[eventTypesTable insertRowsAtIndexPaths:cells withRowAnimation:UITableViewRowAnimationNone]; 
[eventTypesTable endUpdates]; 

उत्तर

18

मुझे लगता है कि आप गलत दिशा से इस पर पहुंच रहे हैं। UITableViews काम नहीं कर रहे हैं जैसा आप उम्मीद कर रहे हैं। insertRowsAtIndexPaths पहले उदाहरण में पॉप्युलेट करने के बजाए, तालिका में नई पंक्तियों को डालने के लिए है।

UITableViews कई प्रतिनिधि विधियों को कॉल करके काम करते हैं जो आपको तालिका दृश्य में अपना डेटा पेश करने की अनुमति देते हैं। फ्रेमवर्क तब कोशिकाओं को पॉप्युलेट करने, स्क्रॉलिंग और टच इवेंट इत्यादि को संभालने के लिए भारी उठाने का ख्याल रखता है।

मैं आपको एक ट्यूटोरियल पढ़ने से शुरू करता हूं जैसे कि: http://www.iosdevnotes.com/2011/10/uitableview-tutorial/, जो मुझे काफी अच्छी तरह से दिखता है। यह बताता है कि टेबल के लिए अपना डेटासोर्स कैसे सेट करें और आप UITableView द्वारा अपना डेटा किस तरह प्रस्तुत कर सकते हैं।

शुभकामनाएं!

+0

धन्यवाद, मैं देख रहा हूँ मैं पूरी तरह से के बारे में है कि कैसे काम उलझन में था। मैंने अब कोशिकाओं को सही ढंग से आउटपुट करने में कामयाब रहा है लेकिन मुझे एक समस्या हो रही है। मैं डेटाबेस से 12 पंक्तियों को पुनर्प्राप्त कर रहा हूं, हालांकि, स्क्रीन केवल 7 फिट बैठती है। एक बार जब स्क्रीन पूर्ण हो जाती है और सिम्युलेटर में मैं स्क्रॉल करने का प्रयास करता हूं तो मुझे 'NSString * sEventType = [[eventTypes valueForKeyPath: @" नाम में त्रुटि मिलती है .text "] objectAtIndex: indexPath.row];' 'cellForRowAtIndexPath' विधि के अंदर। क्या मैं कुछ छोड़ रहा हूं? जैसे मुझे स्क्रॉल नियंत्रक या कुछ जोड़ना है? एक बार फिर, शीघ्र और सहायक प्रतिक्रिया के लिए धन्यवाद! – KerrM

+7

लिंक टूटा हुआ है। यही कारण है कि आपको कम से कम बाहरी लिंक पर भरोसा करने के बजाय कुछ नमूना कोड पोस्ट करना चाहिए। –

+0

लिंक टूटा हुआ है, लेकिन आप यहां इस यूआरएल की सामग्री पा सकते हैं: http://web.archive.org/web/20150928131750/http://www.iosdevnotes.com/2011/10/uitableview-tutorial/ – Upsilon42

0

आपको इसके बारे में चिंता करने की ज़रूरत नहीं है। कोशिकाओं को स्वचालित रूप से बनाया जाएगा। बस इन UITableview Class Reference

Tableview_iPhone

आप UITableView डेटा स्रोत और प्रतिनिधि प्रोटोकॉल को लागू करने के लिए है देखो। यह ट्यूटोरियल UITableview Tutorial

16

insertRowsAtIndexPaths का उपयोग करने की आवश्यकता नहीं है।

की जांच: UITableViewDataSource Protocol Reference और UITableView Class Reference

जादू इस तीन तरीकों (UITableViewDataSource प्रोटोकॉल विधि) के बीच होता है:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 

तुम बस एक सरणी भरने की जरूरत है। हां यह NSMutableArray हो सकता है।

आप उदाहरण के लिए, - (void)viewDidLoad में सरणी भर सकते हैं:

yourItemsArray = [[NSMutableArray alloc] initWithObjects:@"item 01", @"item 02", @"item 03", nil]; 

और इस तरह से उन्हें डेटा स्रोत तरीकों का उपयोग:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    // If You have only one(1) section, return 1, otherwise you must handle sections 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return [yourItemsArray count]; 
} 

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

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

    // Configure the cell... 
    cell.textLabel.text = [NSString stringWithFormat:[yourItemsArray objectAtIndex:indexPath.row]]; 

    return cell; 
} 

इस कोशिकाओं की तरह स्वचालित रूप से बनाया जाएगा।

आप सरणी chage, बस कॉल करने की आवश्यकता:

[self.tableView reloadData]; 
+0

धन्यवाद आपके उत्तर के लिए, काश मैं एक से अधिक उत्तर एक स्वीकृत के रूप में चुन सकता हूं। – KerrM

+3

यह ठीक है, एक ही समस्या के लिए अलग-अलग दृष्टिकोण हैं। और यदि आप सीखना शुरू कर रहे हैं, तो आपको यह समझने के लिए ट्यूटोरियल लेना चाहिए कि चीजें कैसे काम करती हैं, सिर्फ कोड ही नहीं। लेकिन दोनों वास्तव में – Frade

+0

मदद करते हैं हां, मुझे लगता है कि वहां होगा। हालांकि, मुझे उम्मीद है कि आप मदद कर सकते हैं - मैं डेटाबेस से 12 पंक्तियों को पुनः प्राप्त कर रहा हूं, हालांकि स्क्रीन केवल फिट बैठती है। स्क्रीन स्क्रीन भरने के बाद और सिम्युलेटर में अगर मैं स्क्रॉल करने का प्रयास करता हूं तो मुझे एक मिलता है एनएसएसटींग में त्रुटि * sEventType = [[eventTypes valueForKeyPath: @ "name.text"] objectAtIndex: indexPath.row]; cellForRowAtIndexPath विधि के अंदर। स्क्रीन की निचली हिस्से में पहुंचने के बाद यह लगभग चलने वाला तरीका बंद हो जाएगा। क्या यह कुछ मैंने गलत किया है? – KerrM

2
//######## Adding new section programmatically to UITableView ############ 

    @interface MyViewController : UIViewController<UITableViewDataSource,UITableViewDelegate> 
    { 
     IBOutlet UITableView *tblView; 
     int noOfSection; 
    } 
    -(IBAction)switchStateChanged:(id)sender; 
    @end 



    @implementation MyViewController 
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ 
     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
     if (self) { 
      // Custom initialization 
     } 
     return self; 
    } 
    - (void)viewDidLoad{ 
     [super viewDidLoad]; 

     noOfSection = 2; 
    } 
    - (void)viewDidUnload{ 
     [super viewDidUnload]; 
    } 
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ 
     if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { 

      return YES; 
     } 

     return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
    } 
    #pragma mark - TableView Delegate Methods 
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 
     return noOfSection; 
    } 
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 

     return 1; 
    } 
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
     if(indexPath.section == 2){ 
      return 200; 
     } 
     return 50; 
    } 

    - (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]; 

      UISwitch *switchBtn = [[UISwitch alloc] initWithFrame:CGRectMake(0, 0, 20, 10)]; 
      cell.accessoryView = switchBtn; 

      [switchBtn addTarget:self action:@selector(switchStateChanged:) forControlEvents:UIControlEventValueChanged]; 
      cell.textLabel.font = [UIFont systemFontOfSize:14]; 
      cell.detailTextLabel.font = [UIFont systemFontOfSize:11]; 
      cell.textLabel.numberOfLines = 2; 
      cell.detailTextLabel.numberOfLines = 2; 
     } 



     if(indexPath.section == 0){ 
      cell.textLabel.text = @"Cell-1 Text"; 
      cell.detailTextLabel.text = @"Cell-1 Detail text"; 
     } 
     else if(indexPath.section == 1){ 
      cell.textLabel.text = @"Cell-2 Text"; 
     } 
     else { // new added section code is here... 
      cell.textLabel.text = @"New Added section"; 
     } 
     [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
     return cell; 
    } 
    -(IBAction)switchStateChanged:(id)sender{ 
     UISwitch *switchState = sender; 

     if(switchState.isOn == YES){ 
      NSLog(@"ON"); 
      NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:2]; 
      [self insertNewSectionWithIndexPath:indexPath]; 
     } 
     else { 
      NSLog(@"OFF"); 
      [self removeSectionWithIndexPath:[NSIndexPath indexPathForRow:0 inSection:2]]; 
     } 
    } 
    -(void)insertNewSectionWithIndexPath:(NSIndexPath *)indexPath{ 


     noOfSection = 3; 
     [tblView insertSections:[NSIndexSet indexSetWithIndex:2] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
    -(void)removeSectionWithIndexPath:(NSIndexPath *)indexPath{ 
     noOfSection = 2; 
     [tblView deleteSections:[NSIndexSet indexSetWithIndex:2] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
    @end