2012-01-18 18 views
6

कस्टमकेल.एम में प्रारंभ करता हूं I in init विधि को परिभाषित करता हूं जहां मैं से सेल लोड करना चाहता हूं आईबी:'स्वयं' लौटने पर 'आत्म (स्वयं या स्वयं) init ...]' के परिणाम पर सेट नहीं किया गया है जब मैं कस्टम सेल

- (id)init { 
    self = [super init]; 
    if (self) { 
     NSArray *nib =[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 
     self = [nib objectAtIndex:0]; 

    } 
    return self; 
} 

विधि cellForRowAtIndexPath में MyTableViewController.m में मैं अपने कस्टम सेल

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

cell=[[CustomCell alloc]init]; 
return cell; 

} को प्रारंभ

सब कुछ काम करता है के रूप में मैं उम्मीद लेकिन जब मैं Product -> Analyse मैंने किया
Returning 'self' while it is not set to the result of '[(super or self) init...]'
मैं क्या गलत कर रहा हूं?

उत्तर

8

आप अपने सरणी से लौटाई गई वस्तु के साथ self (super init से लौट आए हैं) को ओवरराइट कर रहे हैं। यदि आप एक निब से एक कस्टम सेल लोड करना चाहते हैं, तो आपके cellForRowAtIndexPath विधि में करते हैं या फिर कि निब से लोड करता है अपने कस्टम सेल पर एक सुविधा वर्ग विधि बनाने के लिए:

अपने cellForRowAtIndexPath में:

cell = [CustomCell cell]; 

बदल विधि नाम नई * संकेत के बाद से है कि एक बनाए रखा वस्तु लौटा दी जाएगी -

+(CustomCell*)cell 
{ 
    NSArray *nib =[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];   
    return [nib objectAtIndex:0]; 
} 

संपादित: अपने सेल के क्रियान्वयन में।

+0

धन्यवाद, लेकिन सेल = [कस्टमसेल न्यूसेल] से पहले; क्या मुझे init सेल की आवश्यकता है: सेल = [[कस्टमसेल alloc] init]; – Alex

+0

जैसा आपने समझाया मैंने किया, और अब मुझे यह मिला! पिछले की बजाय: ऑब्जेक्ट +0 के साथ ऑब्जेक्ट कॉलर को वापस लौटाया गया जहां एक +1 (स्वामित्व) गिनती बरकरार रखी जाती है – Alex

+0

ओह हाँ, मैंने इसे नया सेल कहा है जो एआरसी शिकायत करेगा। मैं जवाब अपडेट कर दूंगा, क्षमा करें। – jrturton

7

नीचे के रूप में अपने init विधि रखें, और इंटरफ़ेस बिल्डर

- (id)init { 
    self = [super init]; 
    if (self) { 

    } 
    return self; 
} 

और

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"CustomCell"; 

    CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 

     for (id currentObject in topLevelObjects){ 
      if ([currentObject isKindOfClass:[UITableViewCell class]]){ 
       cell = (CustomCell *) currentObject; 
       break; 
      } 
     } 
    } 
} 
+1

प्रतिक्रिया के लिए धन्यवाद, मुझे यह विधि पता है लेकिन मैं सेल के अंदर विधि init करना पसंद करता हूं। – Alex

1

में जोड़ने क्या मैं कर रहा हूँ मुख्य में तो

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) 
    { 
     // Initialization code. 
     // 
     UITableViewCell *view = [[[NSBundle mainBundle] loadNibNamed:@"SmallCellView" owner:self options:nil] lastObject]; 
     self.backgroundView = view; 
} 
    return self; 
} 

है कक्षा

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    SmallCellView *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[SmallCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 
    return cell; 
} 

मुझे इस के लिए ठीक काम कर रहा है और Product -> Analyse में किसी चेतावनी या त्रुटि

1

मैं एक ही समस्या से मुलाकात न जताए, मैं यह कोड है कि CustomView की परिभाषा से

NSArray *nib =[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];   
return [nib objectAtIndex:0]; 

तरह हटाने के द्वारा तय कर दी है init विधि।

इन कोड को उस स्थान पर रखें जहां आप कस्टम बनाते हैं।

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