2013-10-22 5 views
6

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

मेरा एलीस्ट म्यूटेबल ऐरे में होमवर्क के प्रकार शामिल हैं। तालिका की प्रत्येक पंक्ति में, मैं प्रदर्शित करने के लिए (ये हैं चर पहले से ही अपने घर का पाठ मॉडल में सेट) हैं:

-ClassName

-AssignmentTitle

-DueDate

यहाँ क्या है मैं वर्तमान में

TableViewController.h

@interface AssignmentTableController : UITableViewController 
< 
AssignmentDelegate 
> 

@property(strong,nonatomic) Assignment *vc; 
@property (strong, nonatomic) NSMutableArray *alist;//array was filled with delegation 


@end 

TableViewController.m

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 

// Return the number of sections. 
return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

// Return the number of rows in the section. 
return [self.alist count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier  forIndexPath:indexPath]; 
Homework *ai = [self.alist objectAtIndex:indexPath.row]; 

//*******I am not sure what to do from Here****** 
//*******I need to start displaying 1 object per row,displaying what was stated above*** 

    // Configure the cell... 

return cell; 
} 
+1

संभावित डुप्लिकेट [सरणी से UITableView पॉप्युलेटिंग] (http://stackoverflow.com/questions/4367393/populating-a-uitableview-from-arrays) –

उत्तर

7

मैं एक ऐसी ही कार्यान्वयन है समायोजित करने के लिए की आवश्यकता होगी।

कार्यान्वयन: मैंने निम्न विधियां बनाई हैं। (मैं उन्हें संपादित किया है अपने कोड प्रदान फिट करने के लिए।)

-(Homework*) homeworkAtIndex: (NSInteger) index 
{ 
    return [alist objectAtIndex:index] ; 
} 

-(NSInteger) numberOfObjectsInList 
{ 
    return [alist count]; 
} 

और UITableViewController प्रतिनिधि विधि में:

- (NSInteger) tableView: (UITableView*) tableView numberOfRowsInSection: (NSInteger) section 

मैं इस विधि कॉल

return [self numberOfObjectsInList]; 

प्रतिनिधि विधि में प्रयोग किया है:

- (UITableViewCell*) tableView: (UITableView*) tableView cellForRowAtIndexPath: (NSIndexPath*) indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:SubtitleIdentifier] autorelease]; 
    Homework *ai = [self homeworkAtIndex: indexPath.row]; 

    /* your other cell configurations 
    cell.textLabel.text = ai.className; // eg. display name of text 
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ | %@",ai.assignmentTitle,ai.dueDate]; // will show the cell with a detail text of "assignment title | due date" eg. "Lab 1 | 23 Oct 2013" appearing under the className called "Physics" 
    */ 
} 

विधि का उपयोग होमवर्क एट इंडेक्स आपको सरणी में विभिन्न ऑब्जेक्ट्स को तालिका में विभिन्न कक्षों में पॉप्युलेट करने की अनुमति देगा।

इस तरह से मुझे कस्टम सेल बनाने और तालिका फिट करने के लिए सेल आकार स्वरूपण करने के माध्यम से मिला। शायद यह आपके लिए काम कर सकता है यदि दिखाया जा रहा डेटा इतना लंबा नहीं है, और वास्तव में कस्टम कोशिकाओं का उपयोग नहीं करना है।(बहुत उदाहरण है कि मैं प्रदान की गई)

आपके द्वारा चयनित विभिन्न कोशिकाओं के लिए जाँच करने के लिए कैसे (के रूप में आप उसके बाद एक अलग ViewController करने के लिए उन्हें पुश करने के लिए इच्छा हो सकती है) सोच रहे थे, तो आप प्रतिनिधि विधि में ऐसा कर सकते हैं:

- (void) tableView: (UITableView*) tableView didSelectRowAtIndexPath: (NSIndexPath*) indexPath 
{ 
    UIViewController* nextViewController = nil; 
    NSString* cellDisplayName = [delegate homeworkAtIndex: indexPath.row].name; // can be the Homework object if you want 
    if([cellDisplayName isEqualToString:[self homeworkAtIndex:0].className]) 
     nextViewController = [[firstViewController alloc] init]; 
    else if([cellDisplayName isEqualToString:[self homeworkAtIndex:1].className]) 
     nextViewController = [[secondViewController alloc] init]; 
    // goes on for the check depending on the number of items in the array 
} 

मैं कक्षा नाम की जांच के लिए यहां एनएसएसटींग का उपयोग कर रहा हूं क्योंकि मुझे यकीन नहीं है कि होमवर्क ऑब्जेक्ट क्या है, लेकिन आप निश्चित रूप से होमवर्क ऑब्जेक्ट को फिट करने के लिए तर्क बदल सकते हैं, क्योंकि मेरा अनुमान है कि आपके पास अलग-अलग ऑब्जेक्ट्स हो सकते हैं एक ही कक्षा नाम परिवर्तनीय।

आशा है कि इससे मदद मिलती है! :)

0

एक कस्टम सेल

http://www.appcoda.com/customize-table-view-cells-for-uitableview/

का प्रयोग कर एक दृश्य के रूप में सेल का प्रयोग, बस सेल की सामग्री को देखने पर अपने लेबल की स्थिति [1] मैं आप देख पहले से ही उस डेटा के साथ ऑब्जेक्ट है जिसे आप प्रदर्शित करना चाहते हैं, इसलिए उन चीज़ों के साथ सेल का उपयोग करें जिन्हें आप दिखाना चाहते हैं

3

वहां बहुत से ट्यूटोरियल हैं जो सरणी का उपयोग करके टेबलव्यू पॉप्युलेट करते हैं रों। इन ट्यूटोरियल पर एक नज़र डालें।

http://www.appcoda.com/ios-programming-tutorial-create-a-simple-table-view-app/

http://www.raywenderlich.com/1797/ios-tutorial-how-to-create-a-simple-iphone-app-part-1

आप कस्टम सेल लागू करने के लिए 3 लेबल .. डिफ़ॉल्ट तालिका कोशिकाओं को आमतौर पर 1 छवि देख सकते हैं और दो लेबल ..

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