2011-02-08 9 views
5

के लिए डेटासोर्स के रूप में एनएसएमयूटेबलएरे का उपयोग कैसे करूं, मेरे पास डेटा (हार्ड कोडेड) के साथ एक एनएसएमयूटेबलएरे है।मैं अपने UITableView

मैं इन दो tableview तरीकों को लागू किया और आईबी में, मैं प्रतिनिधि और डेटा स्रोत

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"]; 

    if (!cell) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"] autorelease]; 
    } 

    cell.textLabel.text = [myArray objectAtIndex:[indexPath row]]; 
    NSLog(@"Cell is %@", [myArray objectAtIndex:indexPath.row]); 
    return cell; 
} 

डेटा tableview में दिखाई नहीं देंगे निर्धारित किया है। मैं NSLog भाग गया है और मैं देख सकता हूं कि डेटा myArray में है।

मैंने कोड में एनएसएलॉग जोड़ा और ऐसा लगता है कि कोड कभी निष्पादित नहीं होता है। सवाल यह है कि मेरी सरणी कैसे बनाई जा रही है?

यहाँ कोड

- (id)init:(NSMutableArray *)theArray 
{ 
    [super init]; 
    countryTable.delegate = self; 
    countryTable.dataSource = self; 

    UITapGestureRecognizer * tapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)] autorelease]; 
    [window addGestureRecognizer:tapRecognizer]; 

    myArray = [[NSMutableArray alloc] init]; 
    myArray = theArray; 
    return self; 
} 
+0

आप 'एनएसएमयूटेबलएरे' कैसे बना रहे हैं? – WrightsCS

उत्तर

5

आपका बुनियादी कोड सही है है, बस एक सरणी (जो आप यहाँ प्रदान नहीं किया है) की आवश्यकता है, तो यह एक उदाहरण होगा:

NSMutableArray *myArray = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil]; 

और आप इस NSLog कर सकते हैं: NSLog(@"Count: %i, Array: %@",[myArray count], myArray);

आउटपुट: कौन सा cell.textLabel.textअपने उदाहरण के अनुसार लौट जाना

गणना: 3, सरणी: ( "एक", "दो", "तीन" )

तुम भी चाहिए सुनिश्चित करें कि आपने अपने शीर्षलेख में UITableViewDelegate और UITableViewDataSource प्रोटोकॉल सेट किए हैं।

0

इस प्रयास करें -

cell.textLabel.text = [[myArray objectAtIndex:indexPath.row] retain]; 
+1

UILabel.text एक प्रतिलिपि संपत्ति है, अतिरिक्त रखरखाव सिर्फ एनएसएसटींग को रिसाव करेगा। –

+0

मैंने बनाए रखने और एक ही मुद्दे की कोशिश की। मैं ऊपर अपना अद्यतन कोड पोस्ट करने जा रहा हूँ –

1

अगर सवाल अभी भी खुला है मैं नहीं जानता, लेकिन मैं इसे आजमाइए।

जैसा कि मैंने आपका कोड पढ़ा है, मैं नहीं देख सकता कि देश कहां तत्काल है जहां मुझे लगता है कि यह इंटरफेसबिल्डर में जुड़ा हुआ है? और यदि यह मामला है, तो देशटेबल केवल देखने के बाद मान्य है। लेकिन आप इंटरफ़ेसबिल्डर में प्रतिनिधि और डेटा स्रोत भी सेट कर सकते हैं।

वैसे, प्रत्येक बार जब आप डेटास्रोत (myArray) बदलते हैं, तो CountryTable.reloadData पर कॉल करें।