2009-09-10 19 views
52

में 2 विभिन्न प्रकार के कस्टम UITableViewCells मेरे UITableView में मैं एक आरएसएस फ़ीड की पहली खबर के लिए एक कस्टम टेबल व्यूसेल (टाइप ए चलो कहना) और अन्य समाचार के लिए दूसरे, तीसरे आदि के लिए सेट करना चाहता हूं .. एक और कस्टम टेबल व्यूसेल (कोशिश करें बी) समस्या यह है कि पहली तालिका के लिए बनाई गई कस्टम तालिका व्यूसेल (trype ए) का पुन: उपयोग किया जाता है, लेकिन उत्सुकता से कस्टमव्यूसेल (प्रकार ए) के पहले उपयोग के बीच पंक्तियों की संख्या और उसी प्रकार की कस्टम व्यूसेल की दूसरी उपस्थिति है बराबर नहीं ..UITableView

मेरा सेलफॉररोएट इंडेक्सपैथ ऐसा लगता है।

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

    int feedIndex = [indexPath indexAtPosition:[indexPath length] - 1]; 
    Feed *item = [[[[self selectedButton] category] feedsList] objectAtIndex:feedIndex + 1]; 
    static NSString *CellIdentifier = @"Cell"; 

    if(feedIndex == 0){ 
     MainArticleTableViewCell *cell = (MainArticleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

     if (cell == nil) 
     { 
      cell = [[[MainArticleTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
      [[[cell subviews] objectAtIndex:0] setTag:111]; 
     } 

     cell.feed = item; 

     return cell; 

    } 
    else{ 
     NewsTableViewCell *cell = (NewsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

     if (cell == nil) 
     { 
      cell = [[[NewsTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier orientation:currentOrientation] autorelease]; 
      [[[cell subviews] objectAtIndex:0] setTag:111]; 
     } 

     cell.feed = item; 

     return cell; 

    } 
    return nil; 
}  

दो प्रकार की कोशिकाओं में अलग-अलग ऊंचाई होती है जो सही ढंग से सेट की जाती हैं। क्या कोई मुझे सही दिशा में इंगित कर सकता है कि एक कस्टम सेल को केवल पहली खबर के लिए कैसे दिखाना है (पुन: उपयोग नहीं किया जा रहा है)? धन्यवाद

+0

में के लिए

static NSString *MainArticleIdentifier = @"MainArticle"; static NSString *NewsIdentifier = @"News"; 

इसके अलावा, मैं कुछ भी की तरह कभी नहीं देखा है इस प्रश्न का स्विफ्ट संस्करण देखें [यहां] (http://stackoverflow.com/ प्रश्न/30774671/यूटेबलव्यू-एक-से-एक-कस्टम-सेल्स-स्विफ्ट के साथ) – Honey

उत्तर

78

आप सेल के दो शैलियों के लिए एक अलग सेल पहचानकर्ता बनाना चाहिए:

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

int feedIndex = [indexPath indexAtPosition:[indexPath length] - 1]; 
Feed *item = [[[[self selectedButton] category] feedsList] objectAtIndex:feedIndex + 1]; 

static NSString *CellIdentifier1 = @"Cell1"; 
static NSString *CellIdentifier2 = @"Cell2"; 

if(feedIndex == 0) { 

    MainArticleTableViewCell *cell = (MainArticleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; 

    if (cell == nil) { 
     cell = [[[MainArticleTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier1] autorelease]; 
     [[[cell subviews] objectAtIndex:0] setTag:111]; 
    } 

    cell.feed = item; 

    return cell; 
} 
else { 
    NewsTableViewCell *cell = (NewsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2]; 

    if (cell == nil) { 
     cell = [[[NewsTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier2 orientation:currentOrientation] autorelease]; 
     [[[cell subviews] objectAtIndex:0] setTag:111]; 
    } 

    cell.feed = item; 

    return cell; 
} 

return nil; 
} 
+0

धन्यवाद, यह आपको काम करता है। क्या आपको पता है कि तालिका के विभिन्न वर्गों के लिए अलग-अलग कस्टमकल्स सेट करना संभव है? सेक्शन 0 में टाइप ए और सेक्शन 1 में टाइप बी होगा .. धन्यवाद –

+0

ने answear पाया .. (NSIndexPath *) indexPath में पंक्ति संपत्ति भी अनुभाग संपत्ति है जो उपयोगी थी :) Sorin –

+0

int feedIndex = indexPath.row; - पहली पंक्ति –

8

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

int feedIndex = [indexPath indexAtPosition:[indexPath length] - 1]; 

क्यों नहीं बस का उपयोग करें::

int feedIndex = indexPath.row; 
0

cellForRowAtIndexPath

if ("Condition for cell 1") { 
     cellV = ("customCell" *)[tableView dequeueReusableCellWithIdentifier:@"your ID cell in .xib"]; 

     if (cellV == nil) { 
      [[NSBundle mainBundle] loadNibNamed:@"YOUR-CELL-FILENAME" owner:self options:nil]; 
      cellV = "OUTLET-CEll-IN-VC"; 
     } 
    } else { 
     cellV = ("customCell" *)[tableView dequeueReusableCellWithIdentifier:@"your ID cell2 in .xib"]; 

     if (cellV == nil) { 
      [[NSBundle mainBundle] loadNibNamed:@"YOUR-CELL2-FILENAME" owner:self options:nil]; 
      cellV = "OUTLET-CEll-IN-VC"; 
     } 
    } 

[self configureCell:cellV indexpath:indexPath withClipVo:clip]; 

return cellV;