2015-11-17 11 views
5

में प्रत्येक अनुभाग के लिए अलग-अलग कस्टम सेल का उपयोग करें, मुझे मेरी तालिका पर कुछ अजीब लगता है। मैं दो या दो से अधिक अनुभाग के साथ टेबल बनाना चाहता हूं, और पहले खंड पर मैं दूसरों के साथ अलग-अलग कस्टम सेल का उपयोग करना चाहता हूं।UITableView

तो मैं पहले खंड पर मेरी tableView:cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"cell"; 
    if (indexPath.section == 0) { 
     // cell for section one 
     HeaderCell *headerCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
     if(!headerCell) { 
      [tableView registerNib:[UINib nibWithNibName:@"HeaderCell" bundle:nil] forCellReuseIdentifier:cellIdentifier]; 
      headerCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
     } 
     headerCell.labelName.text = @"First Section"; 
     return headerCell; 
    } 
    else { 
     // Cell for another section 
     DetailCell *detailCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
     if (!detailSection) { 
      [tableView registerNib:[UINib nibWithNibName:@"DetailCell" bundle:nil] forCellReuseIdentifier:cellIdentifier]; 
      detailCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
     } 
     detailCell.textLabel.text = @"Another Section Row"; 
     return detailCell; 
    } 
} 

पर इस बनाया है, मैं अपने पंक्ति के लिए headerCell उपयोग करने के लिए है, तो दूसरों पर detailCell का उपयोग करना चाहते हैं। यह कोड काम करता है लेकिन सेक्शन दो की पंक्ति पर headerCell "detailCell" के तहत अभी भी उपयोग की तरह दिखता है। मैंने headerCell.xib में लेबल जोड़ा, और यह अभी भी detailCell पर दिखाया गया है। यह image देखें।

मुझे यह सब लगता है क्योंकि मैं सभी अनुभागों के लिए एक सेल पहचानकर्ता का उपयोग करता हूं। किसी के पास समाधान है? बहुत बहुत धन्यवाद।

+0

हाँ, आप 2 अलग सेल पहचानकर्ता का उपयोग करने की आवश्यकता है। – Gandalf

उत्तर

9

प्रत्येक प्रकार के कस्टम सेल का अपना अनूठा पहचानकर्ता होना चाहिए। आपका कोड सभी कोशिकाओं के लिए एक ही सेल पहचानकर्ता का उपयोग करने का प्रयास कर रहा है। वह काम नहीं करेगा।

इसके अलावा, viewDidLoad में दो सेल प्रकार पंजीकृत करें, cellForRowAtIndexPath: नहीं।

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

static NSString *cellIdentifier0 = @"cell0"; 
static NSString *cellIdentifier1 = @"cell1"; 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.section == 0) { 
     // cell for section one 
     HeaderCell *headerCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier0 forIndexPath:indexPath]; 

     headerCell.labelName.text = @"First Section"; 

     return headerCell; 
    } else { 
     // Cell for another section 
     DetailCell *detailCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier1 forIndexPath:indexPath]; 

     detailCell.textLabel.text = @"Another Section Row"; 

     return detailCell; 
    } 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    // the rest of your code 

    [self.tableView registerNib:[UINib nibWithNibName:@"HeaderCell" bundle:nil] forCellReuseIdentifier:cellIdentifier0]; 
    [self.tableView registerNib:[UINib nibWithNibName:@"DetailCell" bundle:nil] forCellReuseIdentifier:cellIdentifier1]; 
} 
+0

आपको बहुत धन्यवाद, rmaddy! –