2011-11-10 27 views
47

मैंने एप्पल दस्तावेज पढ़ा है और Objective-C में इस तरह के शुरुआती के लिए यह समझ में नहीं आता है। मैं इस link उदाहरण के बाद मल्टीकॉलम UITableView को कार्यान्वित करने की कोशिश कर रहा हूं और यह अभी काम नहीं करता है इसलिए मुझे यह समझने की आवश्यकता है कि cellForRowAtIndexPath काम कैसे करें, मेरे लिए व्यक्तिगत रूप से यह विधि बहुत जटिल लगती है।CellForRowAtIndexPath कैसे काम करता है?

1) यह क्या लौटता है? UITableViewCell? लेकिन यह इतना अजीब क्यों दिखता है?

-(UITableViewCell *)tableView:(UITableView *)tableView 
  • कि क्या है? क्या आप कृपया समझा सकते हैं?

2) इसे कैसे कहा जाता है और यह कितना महत्वपूर्ण है कि मैं इसे किसी निश्चित UITableView से कैसे कनेक्ट कर सकता हूं ??? क्या होगा यदि मेरे पास दो UITableView का नाम firstTableView और secondTableView है और मैं उन्हें अलग होना चाहता हूं (cellForRowAtIndexPath अलग-अलग करने के लिए)? कैसे मैं अपने UITableViews इस

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

से लिंक करना चाहिए विधि NSIndexPath, नहीं UITableView स्वीकार करता है। मैं क्या करूंगा?

+0

में बस इस सवाल का मिला है। मुझे पता है कि यह 6 साल का है, लेकिन वैसे भी ... एक उदाहरण विधि को इस प्रकार वर्णित किया गया है: '- (return_type) firstPartOfMethodName: (first_param_type) first_param secondPartOfMethodName: (second_param_type) second_param ...', जहां 'return_type' प्रकार है विधि का मान देता है, 'first_param_type', 'second_param_type', आदि विधि को पारित पैरामीटर के प्रकार हैं,' first_param',' second_param', आदि विधि में पारित वास्तविक चर हैं और 'firstPartOfMethodName: secondPartOfMethodName : ... विधि का नाम (या ** हस्ताक्षर **) है। –

उत्तर

36

1) फ़ंक्शन तालिका तालिका के लिए एक सेल देता है हाँ? इसलिए, लौटाई गई वस्तु UITableViewCell प्रकार है। ये वे वस्तुएं हैं जिन्हें आप तालिका की पंक्तियों में देखते हैं। यह फ़ंक्शन मूल रूप से तालिका दृश्य के लिए एक सेल लौटाता है। लेकिन तुम, पूछ सकते हैं कि कैसे समारोह क्या सेल क्या पंक्ति है, जो 2 प्रश्न

2) NSIndexPath में उत्तर दिया जाता है के लिए वापस जाने के लिए पता होगा अनिवार्य रूप से दो बातें

  • आपका धारा
  • है आपका पंक्ति

क्योंकि आपकी तालिका को कई अनुभागों में विभाजित किया जा सकता है और प्रत्येक अपनी पंक्तियों के साथ, यह NSIndexPath आपको कौन सा अनुभाग और कौन सी पंक्ति की पहचान करने में मदद करेगा। वे दोनों पूर्णांक हैं। यदि आप एक नौसिखिया हैं, तो मैं कहूंगा कि केवल एक सेक्शन के साथ प्रयास करें।

यह कहा जाता है कि क्या आप अपने दृश्य नियंत्रक में UITableViewDataSource प्रोटोकॉल को लागू करते हैं। UITableViewController कक्षा जोड़ने का एक आसान तरीका होगा। मैं दृढ़ता से इसकी अनुशंसा करता हूं क्योंकि ऐप्पल के पास आपके लिए लिखे गए कार्यों को आसानी से कार्यान्वित करने के लिए कुछ कोड लिखा गया है जो तालिका का वर्णन कर सकते हैं। वैसे भी, यदि आप स्वयं को इस प्रोटोकॉल को लागू करना चुनते हैं, तो आपको UITableViewCell ऑब्जेक्ट बनाने और किसी भी पंक्ति के लिए इसे वापस करने की आवश्यकता है। पुन: उपयोगिता को समझने के लिए अपने वर्ग संदर्भ पर नज़र डालें क्योंकि तालिका दृश्य में प्रदर्शित कोशिकाओं को बार-बार पुन: उपयोग किया जाता है (यह एक बहुत ही कुशल डिजाइन बीटीडब्ल्यू है)।

जब आपके पास दो टेबल दृश्य हैं, तो विधि को देखें।तालिका दृश्य इसे पास कर दिया गया है, इसलिए आपको इसके संबंध में कोई समस्या नहीं होनी चाहिए।

92

मैं कोशिश करता हूँ और यह (documention से उदाहरण) को तोड़ने के नीचे

/* 
* The cellForRowAtIndexPath takes for argument the tableView (so if the same object 
* is delegate for several tableViews it can identify which one is asking for a cell), 
* and an indexPath which determines which row and section the cell is returned for. 
*/ 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    /* 
    * This is an important bit, it asks the table view if it has any available cells 
    * already created which it is not using (if they are offScreen), so that it can 
    * reuse them (saving the time of alloc/init/load from xib a new cell). 
    * The identifier is there to differentiate between different types of cells 
    * (you can display different types of cells in the same table view) 
    */ 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"]; 

    /* 
    * If the cell is nil it means no cell was available for reuse and that we should 
    * create a new one. 
    */ 
    if (cell == nil) { 

     /* 
     * Actually create a new cell (with an identifier so that it can be dequeued). 
     */ 

     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"] autorelease]; 

     cell.selectionStyle = UITableViewCellSelectionStyleNone; 

    } 

    /* 
    * Now that we have a cell we can configure it to display the data corresponding to 
    * this row/section 
    */ 

    NSDictionary *item = (NSDictionary *)[self.content objectAtIndex:indexPath.row]; 
    cell.textLabel.text = [item objectForKey:@"mainTitleKey"]; 
    cell.detailTextLabel.text = [item objectForKey:@"secondaryTitleKey"]; 
    NSString *path = [[NSBundle mainBundle] pathForResource:[item objectForKey:@"imageKey"] ofType:@"png"]; 
    UIImage *theImage = [UIImage imageWithContentsOfFile:path]; 
    cell.imageView.image = theImage; 

    /* Now that the cell is configured we return it to the table view so that it can display it */ 

    return cell; 

} 

यह एक DataSource विधि है तो उस पर जो भी वस्तु UITableView की DataSource के रूप में घोषित किया है बुलाया जाएगा। इसे तब कहा जाता है जब तालिका दृश्य को वास्तव में पंक्तियों और अनुभागों (जो आप अन्य डेटा स्रोत विधियों में निर्दिष्ट करते हैं) के आधार पर सेल ऑनस्क्रीन प्रदर्शित करने की आवश्यकता होती है।

+0

चूंकि यह धागा एक अच्छी सौदा जानकारी प्रदान करता है, इसलिए मैं एक बिट जानकारी जोड़ना चाहता हूं जो सहायक भी हो सकता है। यदि आप अपने कोड के कुछ हिस्सों से पुनः लोड करना शुरू करते हैं तो यह विधि भी कहा जाता है। जैसे - (शून्य) reloadRowsAtIndexPaths: (NSArray *) indexPaths withRowAnimation: (UITableViewRowAnimation) एनीमेशन –

+0

क्या आपको 'cell.selectionStyle = UITableViewCellSelectionStyleNone नहीं रखना चाहिए, अगर ब्लॉक से बाहर है? जब मैं अंदर डालता हूं, यह काम नहीं करता है। –

5

असल में यह आपके सेल को डिज़ाइन कर रहा है, सेलफोरोएटिंडेक्सपैथ प्रत्येक सेल के लिए बुलाया जाता है और सेल नंबर indexpath.row द्वारा सेक्शन नंबर और indexpath.section द्वारा सेक्शन नंबर मिलता है। यहां आप किसी लेबल, बटन या टेक्स्टफाईड छवि का उपयोग कर सकते हैं जो आप चाहते हैं जो तालिका में सभी पंक्तियों के लिए अपडेट किया गया हो। बयान

सूचकांक पथ उपयोग पर एक अगर दूसरा सवाल के लिए उत्तर पंक्ति के लिए कक्ष में उद्देश्य सी

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

NSString *CellIdentifier = @"CellIdentifier"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if(tableView == firstTableView) 
    { 
     //code for first table view 
     [cell.contentView addSubview: someView]; 
    } 

    if(tableview == secondTableView) 
    { 
     //code for secondTableView 
     [cell.contentView addSubview: someView]; 
    } 
    return cell; 
} 

में स्विफ्ट 3,0

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{ 
    let cell:UITableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell! 

    if(tableView == firstTableView) { 
    //code for first table view 
    } 

    if(tableview == secondTableView)  { 
    //code for secondTableView 
    } 

    return cell 
} 
+0

आपको NSString * CellIdentifier = @ "CellIdentifier" के लिए स्थिर का उपयोग करना चाहिए; यह प्रतिनिधि गतिशील है, इसलिए कुछ भी स्थिर रूप से घोषित किया जाना चाहिए। – GeneCode

+0

लेकिन स्ट्रिंग नहीं बदलती है, यह हमेशा सेल सेल लोड होने पर "सेलइडिएंटिफायर" होने जा रहा है और कुछ और नहीं, इसलिए सेल का पुन: उपयोग किया जाता है, अगर मैं गलत हूं तो मुझे सही करें। – Koushik

+0

स्ट्रिंग बदल नहीं है, सच है। लेकिन इसे कई बार बनाया जाएगा क्योंकि प्रतिनिधि को कई बार बुलाया जाता है। स्थिर का उपयोग करके स्ट्रिंग एक बार बनाई जाती है और पुन: उपयोग की जाती है। – GeneCode

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