2012-10-25 12 views
15

मैंने तालिका दृश्यों को पॉप्युलेट करने के तरीके पर कुछ ट्यूटोरियल खोजने का प्रयास किया है, लेकिन जो कुछ मैंने पाया है वह पुराना और पुराना वीडियो है। मैंने इसे थोड़ा और हालिया से करने की कोशिश की, और यह काम नहीं करता है। मैंआईओएस में पॉप्युलेटिंग टेबलव्यू सेल 6

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"]; 
    [chemarray addObject:@"2"]; 
    [chemarray addObject:@"test"]; 
    [chemarray addObject:@"3"]; 
    [chemarray addObject:@"science"]; 
} 

और

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

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"  forIndexPath:indexPath]; 

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

इस ट्यूटोरियल के अनुसार, जब चलाने के लिए, यह मेरे सरणी में आइटम प्रदर्शित करना चाहिए, लेकिन मेरे लिए, यह नहीं है! क्या कोई जानता है कि इसे कैसे ठीक करें?

+0

क्या आपने अपना व्यू कंट्रोलर टेबल व्यू के प्रतिनिधि के रूप में सेट किया था? क्या यह सेल बनाता है या नहीं? –

+0

यह सेल – Alexyuiop

उत्तर

24

आप register निब भूल का भी ध्यान रखें/सेल पहचानकर्ता के लिए कक्षा।

फार्म एप्पल UITableView प्रलेखन:

महत्वपूर्ण: आप dequeueReusableCellWithIdentifier:forIndexPath: विधि कॉल करने से पहले registerNib:forCellReuseIdentifier: या registerClass:forCellReuseIdentifier: विधि का उपयोग कर एक वर्ग या निब फ़ाइल रजिस्टर करना होगा।

यहाँ एक सादे UITableViewCell के लिए एक उदाहरण है:

- (void) viewDidLoad { 
    [super viewDidLoad]; 

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"]; 
} 

आप निब/वर्ग विधि dequeueReusableCellWithIdentifier:forIndexPath: अनिवार्य रूप से dequeueReusableCellWithIdentifier: रूप में ही है रजिस्टर नहीं है, तो। और यह स्वचालित रूप से आपके लिए एक उदाहरण नहीं बनायेगा।

+1

मैं कहां और कैसे उपयोग करूं और इसे – Alexyuiop

+0

'viewDidLoad' – rckoenes

+0

में डाल दें धन्यवाद, लेकिन मैं इसे वहां कैसे रखूं? – Alexyuiop

1
  1. प्रतिनिधि को uitableview.delegate = self पर सेट करें;
  2. जांच आप घोषणा प्रोटोकॉल में अगर <UITableViewDelegate, UITableViewDataSource>
  3. अगर आप स्टोरीबोर्ड का उपयोग नहीं करते init करने के लिए सेल अगर सेल == शून्य

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 
    if (cell == nil) { 
         cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    
+0

नहीं बनाता है इसलिए डेक्यू रीयूसेबलसेल .... कॉल बेकार है क्योंकि आप अपने द्वारा बनाए गए सेल को "कैश" नहीं करते हैं? –

3

मैं एक ही समस्या में आया था। यदि आप आईओएस 6 का उपयोग कर रहे हैं, तो मुझे लगता है कि आप एक स्टोरीबोर्ड का उपयोग कर रहे हैं और आपने अपनी कस्टम क्लास के साथ व्यू कंट्रोलर को जोड़ा है।

समाधान फिर स्टोरीबोर्ड में जाना है, अपने टेबलव्यू में सेल के गुणों पर जाएं और पहचानकर्ता मान को "सेल" में बदलें। ViewDidLoad में कक्षा को पंजीकृत करने की कोई आवश्यकता नहीं है।

1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    cell.textLabel.text = [chemarray objectAtIndex:indexPath.row]; 
    return cell; 
} 

सुनिश्चित करें कि आप UITableViewDataSource, UITableViewDelegate अपने .h में है और tableview प्रतिनिधि, डेटा स्रोत और .xib में cellindentifier सेल जुड़े बनाओ।

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