2011-02-06 13 views
24

के साथ कस्टम UITableViewCell बनाने के लिए IOS चरण ग्राफ़िक इंटरफ़ेस को आकर्षित करने के लिए मुझे xib फ़ाइल का उपयोग करके अपना स्वयं का UITableViewCell बनाना होगा ... मेरी नई कक्षा बनाने और मेरे UITableView में उपयोग करने के लिए सही कदम क्या हैं? अग्रिमxib फ़ाइल

उत्तर

6

एक video tutorial कैसे Xcode 4.2 के साथ ऐसा करना दिखाने में

धन्यवाद किया गया है। लेखक ने blog post भी लिखा है।

10

व्यक्तिगत रूप से मुझे लगता है कि reuseIdentifier की बात आने पर दोनों सुझाए गए ट्यूटोरियल में बड़ी कमी है। यदि आप इसे इंटरफ़ेस बिल्डर या इसे गलत तरीके से असाइन करना भूल जाते हैं, तो आप हर बार cellForRowAtIndexPath कहलाते हैं।

जेफ लामारे इस बारे में लिखते हैं और इसे blog post में कैसे ठीक करें। reuseIdentifier के अलावा वह Loading Custom Table-View Cells From Nib Files पर सेब दस्तावेज़ों के समान दृष्टिकोण का उपयोग करता है।

इन सभी लेख मैं निम्नलिखित कोड के साथ आया था पढ़ा होने के बाद:

संपादित करें: आपके पास iOS 5.0 लक्षित कर रहे हैं और उच्च आप Duane Fields' answer

@interface CustomCellWithXib : UITableViewCell 

+ (NSString *)reuseIdentifier; 
- (id)initWithOwner:(id)owner; 

@end 

@implementation CustomCellWithXib 

+ (UINib*)nib 
{ 
    // singleton implementation to get a UINib object 
    static dispatch_once_t pred = 0; 
    __strong static UINib* _sharedNibObject = nil; 
    dispatch_once(&pred, ^{ 
     _sharedNibObject = [UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil]; 
    }); 
    return _sharedNibObject; 
} 

- (NSString *)reuseIdentifier 
{ 
    return [[self class] reuseIdentifier]; 
} 

+ (NSString *)reuseIdentifier 
{ 
    // return any identifier you like, in this case the class name 
    return NSStringFromClass([self class]); 
} 

- (id)initWithOwner:(id)owner 
{ 
    return [[[[self class] nib] instantiateWithOwner:owner options:nil] objectAtIndex:0]; 
} 

@end 

UINib साथ रहना चाहता हूँ अगर (आईओएस 4.0 और बाद में उपलब्ध) का उपयोग यहां सिंगलटन के रूप में किया जाता है, क्योंकि reuseIdentifier का उपयोग किया जाता है, फिर भी सेल को लगभग 10 गुणा या फिर शुरू किया जाता है।

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CustomCellWithXib *cell = [tableView dequeueReusableCellWithIdentifier:[CustomCellWithXib reuseIdentifier]]; 
    if (cell == nil) { 
     cell = [[CustomCellWithXib alloc] initWithOwner:self]; 
    } 

    // do additional cell configuration 

    return cell; 
} 
+0

मेरी राय में, यह सबसे अच्छा तरीका है। इसके लिए धन्यवाद। अगर यह मेरा विषय था, तो मैं इसे प्रतिक्रिया के रूप में चिह्नित करूंगा। –

15

iOS5 में आप नए का उपयोग करना चाहेंगे:

registerNib:forCellReuseIdentifier:

कौन सा मूल रूप से एक ही बात करता है ...

+0

कूल, उस पर ध्यान नहीं दिया है। धन्यवाद! – christoph

+0

यही वह चीज है जिसे मैं याद रखने की कोशिश कर रहा था! अच्छा लगा। – Ash

+1

अधिक विशेष रूप से, इसे अपने दृश्य में जोड़ेंडलोड: [self.tableView registerNib: [UINib nibWithNibName: @ "कस्टमसेल" बंडल: nil] forCellReuseIdentifier: @ "CustomCell"]; –

0

आप बना सकते हैं अब cellForRowAtIndexPath इस तरह दिखता है XIB के साथ कस्टमसेल क्लास जिसे UITableViewCell से विरासत में मिला है। हम टेबलव्यू क्लास। एम फ़ाइल में निम्नलिखित तरीके से श्रेणी जोड़ देंगे। मुझे लगता है कि यह सबसे आसान तरीका है जिसे कस्टम सेल निर्माण के लिए लागू किया जाना चाहिए।

 

    @interface UITableViewCell(NIB) 
    @property(nonatomic,readwrite,copy) NSString *reuseIdentifier; 
    @end 
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
     return 30; 
    } 

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
    static NSString *[email protected]"cell"; 
     CustomCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier]; 
     if(cell==nil) 
     { 
      NSLog(@"New Cell"); 
      NSArray *nib=[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 
      cell=[nib objectAtIndex:0]; 
      cell.reuseIdentifier=identifier; 

     }else{ 
      NSLog(@"Reuse Cell"); 
     } 
     cell.lbltitle.text=[NSString stringWithFormat:@"Level %d",indexPath.row]; 
     id num=[_arrslidervalues objectAtIndex:indexPath.row]; 
     cell.slider.value=[num floatValue]; 
     return cell; 
    } 
    @end 

1

इस ट्यूटोरियल समाप्त करने के लिए सेल की xib और वर्ग फ़ाइलों सभी तरह बनाने की प्रक्रिया से पूरे आधुनिक iOS 5 समाधान के माध्यम से ले जाता है,:

http://mrmaksimize.com/ios/Custom-UITableViewCell-With-NIB/

1

`आप बना सकते हैं .xib फ़ाइल की मदद से तालिका दृश्य में कस्टम कक्ष। सबसे पहले अपने दृश्य नियंत्रक में तालिका दृश्य सेट करें, अपनी कक्षा के साथ एक नई xib फ़ाइल बनाएं और इसे तालिका दृश्य में उपयोग करें।

- (IBAction)moveToSubCategory:(id)sender; 
@property (strong, nonatomic) IBOutlet UILabel *foodCategoryLabel; 
@property (strong, nonatomic) IBOutlet UIImageView *cellBg; 



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



-(UITableViewCell *)tableView:(UITableView *)tableView  cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *simpleTableIdentifier = @"ExampleCell"; 
     ExampleCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
     if (cell == nil) { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ExampleCell" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
    } 
    [cell setTag:indexPath.row]; 
    cell.cellBg.image=[UIImage imageNamed:[photoArray objectAtIndex:indexPath.row]]; 
    cell.foodCategoryLabel.text=[foodCatArray objectAtIndex:indexPath.row]; 
    return cell; 

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