कैसे आप XIB फ़ाइलों से कस्टम UITableViewCells लोड करूं?

वोट
278

सवाल सरल है: कैसे आप कस्टम लोड करते UITableViewCellXIB फ़ाइलों से? ऐसा करने से आप इंटरफ़ेस बिल्डर का उपयोग कर अपनी कोशिकाओं डिजाइन करने के लिए अनुमति देता है। जवाब जाहिरा तौर पर स्मृति प्रबंधन के मुद्दों के कारण सरल नहीं है। यह धागा मुद्दे का उल्लेख है और एक समाधान चलता है, लेकिन है पूर्व एनडीए रिलीज और कोड का अभाव है। यहाँ एक है लंबे धागे कि एक निश्चित जवाब दिए बिना इस मुद्दे पर चर्चा करता है।

यहाँ कुछ कोड मैं का उपयोग किया है है:

static NSString *CellIdentifier = @MyCellIdentifier;

MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
    cell = (MyCell *)[nib objectAtIndex:0];
}

इस कोड का उपयोग करने के लिए, MyCell.m / ज, की एक नई उपवर्ग बनाने UITableViewCellऔर जोड़ने के IBOutletsघटकों आप चाहते हैं के लिए। फिर एक नया खाली XIB फ़ाइल बनाएँ। आईबी में XIB फ़ाइल खोलें, एक जोड़ने UITableViewCellवस्तु, MyCellIdentifier करने के लिए इसके पहचानकर्ता निर्धारित करते हैं, और MyCell को अपने वर्ग स्थापित करने और अपने घटक जोड़ने। अंत में, कनेक्ट IBOutletsघटकों के लिए। ध्यान दें कि हम आईबी में फ़ाइल के मालिक सेट नहीं किया है।

अन्य तरीकों फ़ाइल के मालिक की स्थापना की वकालत और स्मृति लीक की चेतावनी दी है यदि xib एक अतिरिक्त कारखाने वर्ग के माध्यम से लोड नहीं है। मैं के तहत उपकरण / लीक से ऊपर परीक्षण किया है और कोई स्मृति लीक देखा।

तो Xibs से कोशिकाओं को लोड करने के लिए विहित तरीका क्या है? हम फ़ाइल के मालिक सेट करते हैं? हम एक कारखाने की ज़रूरत है? यदि हां, तो क्या कारखाने के लिए कोड की तरह लग रही है? यदि अनेक समाधान कर रहे हैं, के पेशेवरों और उनमें से प्रत्येक के विपक्ष को स्पष्ट करते हैं ...

12/02/2009 को 08:27
का स्रोत उपयोगकर्ता
अन्य भाषाओं में...                            


23 जवाब

वोट
292

सही समाधान यह है:

- (void)viewDidLoad
{
 [super viewDidLoad];
 UINib *nib = [UINib nibWithNibName:@"ItemCell" bundle:nil];
 [[self tableView] registerNib:nib forCellReuseIdentifier:@"ItemCell"];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   // Create an instance of ItemCell
   PointsItemCell *cell =  [tableView dequeueReusableCellWithIdentifier:@"ItemCell"];

return cell;
}
29/11/2012 को 16:57
का स्रोत उपयोगकर्ता

वोट
282

यहां ऐसी दो विधियां जो कर रहे हैं मूल लेखक राज्यों एक आईबी इंजीनियर द्वारा सिफारिश की गई थी

अधिक जानकारी के लिए वास्तविक पोस्ट देखें। मैं विधि # 2 पसंद करते हैं के रूप में यह आसान लगता है।

विधि # 1:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BDCustomCell"];
    if (cell == nil) {
        // Create a temporary UIViewController to instantiate the custom cell.
        UIViewController *temporaryController = [[UIViewController alloc] initWithNibName:@"BDCustomCell" bundle:nil];
        // Grab a pointer to the custom cell.
        cell = (BDCustomCell *)temporaryController.view;
        [[cell retain] autorelease];
        // Release the temporary UIViewController.
        [temporaryController release];
    }

    return cell;
}

विधि # 2:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BDCustomCell"];
    if (cell == nil) {
        // Load the top-level objects from the custom cell XIB.
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"BDCustomCell" owner:self options:nil];
        // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
        cell = [topLevelObjects objectAtIndex:0];
    }

    return cell;
}

अद्यतन (2014): विधि # 2 अभी भी मान्य है, लेकिन इसके लिए कोई प्रलेखन अब और नहीं है। यह में हुआ करता था आधिकारिक डॉक्स लेकिन अब स्टोरीबोर्ड के पक्ष में हटा दिया जाता है।

मैं Github पर एक काम उदाहरण पोस्ट:
https://github.com/bentford/NibTableCellExample

21/12/2009 को 11:19
का स्रोत उपयोगकर्ता

वोट
33

रजिस्टर

IOS 7 के बाद, इस प्रक्रिया (करने के लिए नीचे सरलीकृत किया गया है तेजी से 3.0 ):

// For registering nib files
tableView.register(UINib(nibName: "MyCell", bundle: Bundle.main), forCellReuseIdentifier: "cell")

// For registering classes
tableView.register(MyCellClass.self, forCellReuseIdentifier: "cell")

( नोट ) यह भी कोशिकाओं बनाने के द्वारा प्राप्त है .xibया .stroyboard, फ़ाइलें प्रोटोटाइप कोशिकाओं के रूप में। आप उन्हें एक वर्ग के साथ जोड़ने की आवश्यकता है, तो आप सेल प्रोटोटाइप का चयन करें और इसी वर्ग (का वंशज होना चाहिए जोड़ सकते हैं UITableViewCell, निश्चित रूप से)।

विपंक्ति

और बाद में, (का उपयोग कर dequeued तेज 3.0 ):

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell : UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

    cell.textLabel?.text = "Hello"

    return cell
}

अंतर किया जा रहा है कि इस नई विधि न केवल सेल dequeues, यह भी बनाता है, तो गैर existant (मतलब यह है कि तुम क्या करने की जरूरत नहीं है कि if (cell == nil)धोखाधड़ी), और सेल बस ऊपर के उदाहरण में के रूप में उपयोग करने के लिए तैयार है।

( चेतावनी ) tableView.dequeueReusableCell(withIdentifier:for:)नए व्यवहार करता है, तो आप (बिना एक दूसरे को फोन किया है, indexPath:) आप पुराने व्यवहार है, जिसमें आप के लिए जांच करने की आवश्यकता पाने nilऔर अपने आप उदाहरण यह नोटिस UITableViewCell?दिया गया मान।

if let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? MyCellClass
{
    // Cell be casted properly
    cell.myCustomProperty = true
}
else
{
    // Wrong type? Wrong identifier?
}

और निश्चित रूप से, सेल के जुड़े वर्ग के प्रकार के एक आप के लिए .xib फाइल में परिभाषित है UITableViewCellवैकल्पिक रूप से उपवर्ग, या अन्य रजिस्टर पद्धति का उपयोग करके।

विन्यास

आदर्श रूप में, अपनी कोशिकाओं को पहले से ही बार जब आप उन्हें पंजीकृत द्वारा उपस्थिति और सामग्री पोजीशनिंग (लेबल और छवि दृश्य की तरह) के मामले में विन्यस्त किया गया है, और पर cellForRowAtIndexPathविधि आप बस में उन्हें भरें।

सभी एक साथ

class MyCell : UITableViewCell
{
    // Can be either created manually, or loaded from a nib with prototypes
    @IBOutlet weak var labelSomething : UILabel? = nil
}

class MasterViewController: UITableViewController 
{
    var data = ["Hello", "World", "Kinda", "Cliche", "Though"]

    // Register
    override func viewDidLoad()
    {
        super.viewDidLoad()

        tableView.register(MyCell.self, forCellReuseIdentifier: "mycell")
        // or the nib alternative
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return data.count
    }

    // Dequeue
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "mycell", for: indexPath) as! MyCell

        cell.labelSomething?.text = data[indexPath.row]

        return cell
    }
}

और जाहिर है, यह सब एक ही नाम के साथ ObjC में उपलब्ध है।

21/05/2015 को 01:47
का स्रोत उपयोगकर्ता

वोट
32

शॉन Craver के जवाब में ले लिया और यह थोड़ा साफ।

BBCell.h:

#import <UIKit/UIKit.h>

@interface BBCell : UITableViewCell {
}

+ (BBCell *)cellFromNibNamed:(NSString *)nibName;

@end

BBCell.m:

#import "BBCell.h"

@implementation BBCell

+ (BBCell *)cellFromNibNamed:(NSString *)nibName {
    NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:nibName owner:self options:NULL];
    NSEnumerator *nibEnumerator = [nibContents objectEnumerator];
    BBCell *customCell = nil;
    NSObject* nibItem = nil;
    while ((nibItem = [nibEnumerator nextObject]) != nil) {
        if ([nibItem isKindOfClass:[BBCell class]]) {
            customCell = (BBCell *)nibItem;
            break; // we have a winner
        }
    }
    return customCell;
}

@end

मैं BBCell के सभी मेरी UITableViewCell के उपवर्गों करें, और उसके मानक की जगह

cell = [[[BBDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BBDetailCell"] autorelease];

साथ में:

cell = (BBDetailCell *)[BBDetailCell cellFromNibNamed:@"BBDetailCell"];
22/06/2010 को 03:15
का स्रोत उपयोगकर्ता

वोट
15

मैं प्रयोग किया जाता bentford की विधि # 2 :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BDCustomCell"];
    if (cell == nil) {
        // Load the top-level objects from the custom cell XIB.
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"BDCustomCell" owner:self options:nil];
        // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
        cell = [topLevelObjects objectAtIndex:0];
    }

    return cell;
}

यह काम करता है, लेकिन करने के लिए कनेक्शन के लिए देखना फ़ाइल के मालिक अपने कस्टम UITableViewCell .xib फ़ाइल में।

पास करके owner:selfअपने में loadNibNamedबयान, तो आप सेट UITableViewControllerअपने की फ़ाइल के मालिक के रूप में UITableViewCell

आप खींचें और कार्यों और दुकानों की स्थापना के लिए आईबी में हेडर फाइल करने के लिए छोड़ हैं, तो उन्हें सेट हो जाएगा डिफ़ॉल्ट रूप से फ़ाइल के मालिक के रूप में।

में loadNibNamed:owner:options, एप्पल के कोड अपने पर गुण सेट करने की कोशिश करेंगे UITableViewController, क्योंकि है कि मालिक है। लेकिन आप उन गुणों वहाँ परिभाषित नहीं है, तो आप जा रहा है के बारे में त्रुटि कुंजी मान कोडिंग-संगत :

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason:     '[<MyUITableViewController 0x6a383b0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key myLabel.'

एक घटना के बजाय ट्रिगर किया जाता है, तो आप एक NSInvalidArgumentException मिल जाएगा:

-[MyUITableViewController switchValueDidChange:]: unrecognized selector sent to instance 0x8e9acd0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MyUITableViewController switchValueDidChange:]: unrecognized selector sent to instance 0x8e9acd0'
*** First throw call stack:
(0x1903052 0x15eed0a 0x1904ced 0x1869f00 0x1869ce2 0x1904ec9 0x5885c2 0x58855a 0x62db76 0x62e03f 0x77fa6c 0x24e86d 0x18d7966 0x18d7407 0x183a7c0 0x1839db4 0x1839ccb 0x1f8b879 0x1f8b93e 0x585a9b 0xb904d 0x2c75)
terminate called throwing an exceptionCurrent language:  auto; currently objective-c

एक आसान तरीके को पर अपने इंटरफेस बिल्डर कनेक्शन इंगित करने के लिए है UITableViewCellके बजाय फ़ाइल के मालिक:

  1. फ़ाइल के मालिक पर राइट क्लिक करें कनेक्शनों की सूची ऊपर खींचने के लिए
  2. Command-Shift-4 के साथ स्क्रीन कैप्चर कर (क्षेत्र का चयन करने के लिए ड्रैग कब्जा किया जा करने के लिए)
  3. एक्स बाहर फ़ाइल के मालिक से कनेक्शन
  4. सही वस्तु पदानुक्रम में UITableCell पर क्लिक करें और कनेक्शन को पुन: जोड़ने।
19/03/2012 को 22:41
का स्रोत उपयोगकर्ता

वोट
12

मैं के बाद से मैं इन उत्तरों के किसी भी पसंद नहीं है पोस्ट करने के लिए तय कर लिया है - चीजें हमेशा अधिक आसान हो सकता है और यह अब तक का सबसे संक्षिप्त तरीके से मैंने पाया है।

1. इंटरफ़ेस बिल्डर में अपने xib बिल्ड के रूप में आप इसे पसंद

  • फ़ाइल का सेट मालिक वर्ग NSObject के लिए
  • एक UITableViewCell जोड़ें और MyTableViewCellSubclass को अपने वर्ग सेट - अगर आपकी आईबी दुर्घटनाओं (Xcode में होता है> 4 इस लेखन के रूप में), बस एक UIView का उपयोग Xcode 4 में इंटरफेस कर के अगर आप अभी भी इसके चारों ओर बिछाने है
  • इस सेल के अंदर अपने subviews लेआउट और ज या मीटर में अपने @interface करने के लिए अपने IBOutlet कनेक्शन देते हैं (मीटर मेरी प्राथमिकता है)

2. अपने UIViewController या UITableViewController उपवर्ग में

@implementation ViewController

static NSString *cellIdentifier = @"MyCellIdentier";

- (void) viewDidLoad {

    ...
    [self.tableView registerNib:[UINib nibWithNibName:@"MyTableViewCellSubclass" bundle:nil] forCellReuseIdentifier:cellIdentifier];
}

- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyTableViewCellSubclass *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    ...

    return cell;
}

3. अपने MyTableViewCellSubclass में

- (id) initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        ...
    }

    return self;
}
26/08/2013 को 09:18
का स्रोत उपयोगकर्ता

वोट
8

आप कोशिकाओं बनाने के लिए इंटरफ़ेस बिल्डर का उपयोग कर रहे हैं, तो जाँच आप निरीक्षक में पहचानकर्ता सेट कर लिया है। तो जांच कर लें कि यह एक ही है जब dequeueReusableCellWithIdentifier बुला।

मैं गलती से एक टेबल भारी परियोजना में कुछ पहचानकर्ता सेट करने के लिए भूल गया, और प्रदर्शन परिवर्तन रात और दिन की तरह था।

28/04/2010 को 11:55
का स्रोत उपयोगकर्ता

वोट
7

XIBs से UITableViewCells लोड हो रहा है कोड का एक बहुत बचाता है, लेकिन आम तौर पर भयानक स्क्रॉल गति (वास्तव में, यह XIB लेकिन UIViews है कि इस कारण के अत्यधिक उपयोग नहीं है) का परिणाम है।

मेरा सुझाव है कि आप इस पर एक नज़र डालें: लिंक संदर्भ

12/02/2009 को 15:19
का स्रोत उपयोगकर्ता

वोट
5

यहाँ वर्ग विधि है कि मैं XIBs से बाहर कस्टम कोशिकाओं को बनाने के लिए उपयोग करते आ रहे:

+ (CustomCell*) createNewCustomCellFromNib {

    NSArray* nibContents = [[NSBundle mainBundle]
                            loadNibNamed:@"CustomCell" owner:self options:NULL];

    NSEnumerator *nibEnumerator = [nibContents objectEnumerator];
    CustomCell *customCell= nil;
    NSObject* nibItem = nil;

    while ( (nibItem = [nibEnumerator nextObject]) != nil) {

        if ( [nibItem isKindOfClass: [CustomCell class]]) {
            customCell = (CustomCell*) nibItem;

            if ([customCell.reuseIdentifier isEqualToString: @"CustomCell"]) {
                break; // we have a winner
            }
            else
                fuelEntryCell = nil;
        }
    }
    return customCell;
}

फिर, XIB में, मैं वर्ग के नाम, और पुन: उपयोग पहचानकर्ता निर्धारित किया है। उसके बाद, मैं सिर्फ इतना है कि विधि मेरे विचार नियंत्रक में बजाय कॉल कर सकते हैं की

[[UITableViewCell] alloc] initWithFrame:]

यह बहुत तेजी से पर्याप्त है, और मेरे शिपिंग अनुप्रयोगों में से दो में इस्तेमाल किया जा रहा। यह बुला तुलना में अधिक विश्वसनीय है [nib objectAtIndex:0], और मेरे मन में कम से कम, स्टीफ़न Burlot के उदाहरण से ज्यादा विश्वसनीय है क्योंकि आप की गारंटी कर रहे हैं करने के लिए केवल एक XIB कि सही प्रकार है से बाहर एक दृश्य हड़पने।

12/02/2009 को 14:47
का स्रोत उपयोगकर्ता

वोट
4

सही समाधान यह है

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"CustomCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"CustomCell"];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
    return cell; 
    }
07/09/2016 को 08:33
का स्रोत उपयोगकर्ता

वोट
3

- इस चेक http://eppz.eu/blog/custom-uitableview-cell/ : - वास्तव में सुविधाजनक एक छोटे से वर्ग कि नियंत्रक कार्यान्वयन में एक पंक्ति समाप्त होता है का उपयोग कर रास्ता

-(UITableViewCell*)tableView:(UITableView*) tableView cellForRowAtIndexPath:(NSIndexPath*) indexPath
{
    return [TCItemCell cellForTableView:tableView
                          atIndexPath:indexPath
                      withModelSource:self];
}

यहाँ छवि विवरण दर्ज

09/07/2013 को 10:18
का स्रोत उपयोगकर्ता

वोट
3

निब को पुन: लोड महंगा है। बेहतर एक बार इसे लोड, तो जब आप एक सेल की जरूरत की वस्तुओं का दृष्टांत के लिए। ध्यान दें कि आप UIImageViews आदि निब, यहां तक ​​कि कई कक्षों इस पद्धति का उपयोग जोड़ सकते हैं, (एप्पल के "registerNIB" iOS5 केवल एक ही शीर्ष स्तर वस्तु की अनुमति देता है - बग 10,580,062 "iOS5 tableView registerNib: अधिक प्रतिबंधात्मक"

तो मेरी कोड के नीचे है - आप निब में एक बार पढ़ा (इनिशियलाइज़ की तरह मैंने किया था या viewDidLoad में -।। जो कुछ भी तब से, आप वस्तुओं में निब का दृष्टांत तो एक आप की जरूरत लेने इतना निब लोड हो रहा है की तुलना में अधिक कुशल है बार बार।

static UINib *cellNib;

+ (void)initialize
{
    if(self == [ImageManager class]) {
        cellNib = [UINib nibWithNibName:@"ImageManagerCell" bundle:nil];
        assert(cellNib);
    }
}

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if(cell == nil) {
        NSArray *topLevelItems = [cellNib instantiateWithOwner:nil options:nil];
        NSUInteger idx = [topLevelItems indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop)
                            {
                                UITableViewCell *cell = (UITableViewCell *)obj;
                                return [cell isKindOfClass:[UITableViewCell class]] && [cell.reuseIdentifier isEqualToString:cellID];
                            } ];
        assert(idx != NSNotFound);
        cell = [topLevelItems objectAtIndex:idx];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"Howdie %d", indexPath.row];

    return cell;
}
14/12/2011 को 20:01
का स्रोत उपयोगकर्ता

वोट
2

सबसे पहले अपने कस्टम सेल फ़ाइल आयात #import "CustomCell.h"और फिर प्रतिनिधि पद्धति को बदलने नीचे के रूप में उल्लेख किया:

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

static NSString *simpleTableIdentifier = @"CustomCell";

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

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}         

     return cell;
}
26/02/2014 को 07:48
का स्रोत उपयोगकर्ता

वोट
2

यह करने के लिए सही तरीका एक UITableViewCell उपवर्ग कार्यान्वयन, शीर्षक, और XIB तैयार करना है। XIB में किसी भी दृश्य हटाने और सिर्फ एक तालिका सेल जोड़ें। UITableViewCell उपवर्ग के नाम के रूप में वर्ग निर्धारित करें। फ़ाइल मालिक के लिए, यह UITableViewController उपवर्ग वर्ग के नाम हैं। सेल tableViewCell आउटलेट का उपयोग करने के लिए फ़ाइल मालिक से कनेक्ट करें।

हेडर फाइल में:

UITableViewCell *_tableViewCell;
@property (assign) IBOutlet UITableViewCell *tableViewCell;

कार्यान्वयन फ़ाइल में:

@synthesize tableViewCell = _tableViewCell;

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
    if (cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:kCellIdentifier owner:self options:nil];
        cell = _tableViewCell;
        self.tableViewCell = nil;
    }

    return cell;
}
28/06/2012 को 22:57
का स्रोत उपयोगकर्ता

वोट
2

क्या मैं इस के लिए क्या एक घोषित है IBOutlet UITableViewCell *cellअपने नियंत्रक वर्ग में। फिर आह्वान NSBundle loadNibNamedवर्ग विधि है, जो फीड होगा UITableViewCellसेल ऊपर घोषित करने के लिए।

Xib के लिए मैं एक खाली xib बना सकते हैं और जोड़ देगा UITableViewCellआईबी में वस्तु जहां यह सेटअप रूप में की जरूरत हो सकता है। यह दृश्य तो सेल से जुड़ा है IBOutletनियंत्रक कक्षा में।

- (UITableViewCell *)tableView:(UITableView *)table
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%@ loading RTEditableCell.xib", [self description] );

    static NSString *MyIdentifier = @"editableCellIdentifier";
    cell = [table dequeueReusableCellWithIdentifier:MyIdentifier];

    if(cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"RTEditableCell"
                                      owner:self
                                    options:nil];
    }

    return cell;
}

NSBundle अतिरिक्त loadNibNamed (एडीसी प्रवेश)

cocoawithlove.com लेख मैं से अवधारणा (फोन नंबर नमूना एप्लिकेशन प्राप्त) sourced

12/02/2009 को 19:01
का स्रोत उपयोगकर्ता

वोट
1

में स्विफ्ट 4.2 और Xcode 10

मैं तीन XIB सेल फ़ाइलें

ViewDidLoad में इस तरह अपने XIB फ़ाइलों रजिस्टर ...

यह पहला दृष्टिकोण है

tableView.register(UINib.init(nibName: "XIBCell", bundle: nil), forCellReuseIdentifier: "cell1")
tableView.register(UINib.init(nibName: "XIBCell2", bundle: nil), forCellReuseIdentifier: "cell2")
//tableView.register(UINib.init(nibName: "XIBCell3", bundle: nil), forCellReuseIdentifier: "cell3")

दूसरा दृष्टिकोण सीधे XIB फ़ाइलों रजिस्टर में cellForRowAt indexPath:

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

//MARK: - Tableview delegates
override func numberOfSections(in tableView: UITableView) -> Int {

    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 6
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    //This is first approach
    if indexPath.row == 0 {//Load first XIB cell
        let placeCell = tableView.dequeueReusableCell(withIdentifier: "cell1") as! XIBCell
        return placeCell
    //Second approach
    } else if indexPath.row == 5 {//Load XIB cell3
        var cell = tableView.dequeueReusableCell(withIdentifier:"cell3") as? XIBCell3
        if cell == nil{
            let arrNib:Array = Bundle.main.loadNibNamed("XIBCell3",owner: self, options: nil)!
            cell = arrNib.first as? XIBCell3
        }

        //ADD action to XIB cell button
        cell?.btn.tag = indexPath.row//Add tag to button
        cell?.btn.addTarget(self, action: #selector(self.bookbtn1(_:)), for: .touchUpInside);//selector

        return cell!
    //This is first approach
    } else {//Load XIB cell2
        let placeCell = tableView.dequeueReusableCell(withIdentifier: "cell2") as! XIBCell2

        return placeCell
    }

}
12/10/2018 को 07:02
का स्रोत उपयोगकर्ता

वोट
1
  1. अपने खुद के अनुकूलित कक्षा बनाएं AbcViewCellसे उपवर्ग UITableViewCell(सुनिश्चित करें कि आपके वर्ग फ़ाइल नाम और निब फ़ाइल नाम एक ही हैं)

  2. इस विस्तार के वर्ग विधि बनाएँ।

    extension UITableViewCell {
        class func fromNib<T : UITableViewCell>() -> T {
            return Bundle.main.loadNibNamed(String(describing: T.self), owner: nil, options: nil)?[0] as! T
        }
    }
    
  3. इसका इस्तेमाल करें।

    let cell: AbcViewCell = UITableViewCell.fromNib()

07/09/2017 को 04:07
का स्रोत उपयोगकर्ता

वोट
1

मैं अगर वहाँ एक विहित तरीका है नहीं जानता, लेकिन यहाँ मेरी विधि है:

  • एक ViewController के लिए एक xib बनाएं
  • UIViewController के लिए फ़ाइल मालिक वर्ग सेट करें
  • दृश्य हटाएं और एक UITableViewCell जोड़ने
  • अपने कस्टम कक्षा में अपने UITableViewCell की कक्षा सेट
  • अपने UITableViewCell के पहचानकर्ता सेट
  • अपने UITableViewCell के लिए अपने दृश्य नियंत्रक को देखने के आउटलेट सेट

और इस कोड का उपयोग करें:

MyCustomViewCell *cell = (MyCustomViewCell *)[_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
  UIViewController* c = [[UIViewController alloc] initWithNibName:CellIdentifier bundle:nil];
  cell = (MyCustomViewCell *)c.view;
  [c release];
}

अपने उदाहरण में, का उपयोग करते हुए

[nib objectAtIndex:0]

टूट सकता है, तो एप्पल xib में आइटम के आदेश बदल जाता है।

12/02/2009 को 10:03
का स्रोत उपयोगकर्ता

वोट
0
 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

            let cellReuseIdentifier = "collabCell"
            var cell:collabCell! = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as? collabCell
            if cell == nil {
                tableView.register(UINib(nibName: "collabCell", bundle: nil), forCellReuseIdentifier: cellReuseIdentifier)
                cell = tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! collabCell!
            }


            return cell

}
18/05/2018 को 11:17
का स्रोत उपयोगकर्ता

वोट
0

यहाँ में कोशिकाओं को पंजीकृत करने के लिए एक सार्वभौमिक दृष्टिकोण है UITableView:

protocol Reusable {
    static var reuseID: String { get }
}

extension Reusable {
    static var reuseID: String {
        return String(describing: self)
    }
}

extension UITableViewCell: Reusable { }

extension UITableView {

func register<T: UITableViewCell>(cellClass: T.Type = T.self) {
    let bundle = Bundle(for: cellClass.self)
    if bundle.path(forResource: cellClass.reuseID, ofType: "nib") != nil {
        let nib = UINib(nibName: cellClass.reuseID, bundle: bundle)
        register(nib, forCellReuseIdentifier: cellClass.reuseID)
    } else {
        register(cellClass.self, forCellReuseIdentifier: cellClass.reuseID)
    }
}

स्पष्टीकरण:

  1. Reusableप्रोटोकॉल अपने वर्ग के नाम से सेल आईडी उत्पन्न करता है। सुनिश्चित करें कि आप का पालन करें: cell ID == class name == nib name
  2. UITableViewCellके अनुरूप Reusableप्रोटोकॉल।
  3. UITableView विस्तार निब या वर्ग के माध्यम से कोशिकाओं को पंजीकृत करने में अंतर दूर सार।

प्रयोग उदाहरण:

override func viewDidLoad() {
    super.viewDidLoad()
    let tableView = UITableView()
    let cellClasses: [UITableViewCell.Type] = [PostCell.self, ProfileCell.self, CommentCell.self]
    cellClasses.forEach(tableView.register)
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: PostCell.self.reuseID) as? PostCell
    ...
    return cell
}
18/08/2017 को 08:08
का स्रोत उपयोगकर्ता

वोट
0

इस विस्तार के Xcode7 beta6 की आवश्यकता है

extension NSBundle {
    enum LoadViewError: ErrorType {
        case ExpectedXibToExistButGotNil
        case ExpectedXibToContainJustOneButGotDifferentNumberOfObjects
        case XibReturnedWrongType
    }

    func loadView<T>(name: String) throws -> T {
        let topLevelObjects: [AnyObject]! = loadNibNamed(name, owner: self, options: nil)
        if topLevelObjects == nil {
            throw LoadViewError.ExpectedXibToExistButGotNil
        }
        if topLevelObjects.count != 1 {
            throw LoadViewError.ExpectedXibToContainJustOneButGotDifferentNumberOfObjects
        }
        let firstObject: AnyObject! = topLevelObjects.first
        guard let result = firstObject as? T else {
            throw LoadViewError.XibReturnedWrongType
        }
        return result
    }
}

एक XIB फ़ाइल है कि सिर्फ 1 कस्टम UITableViewCell शामिल बनाएँ।

इसे लोड करें।

let cell: BacteriaCell = try NSBundle.mainBundle().loadView("BacteriaCell")
28/08/2015 को 19:44
का स्रोत उपयोगकर्ता

वोट
0
 NSString *CellIdentifier = [NSString stringWithFormat:@"cell %ld %ld",(long)indexPath.row,(long)indexPath.section];


    NewsFeedCell *cell = (NewsFeedCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell=nil;

    if (cell == nil)
    {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"NewsFeedCell" owner:nil options:nil];

        for(id currentObject in topLevelObjects)
        {
            if([currentObject isKindOfClass:[NewsFeedCell class]])
            {
                cell = (NewsFeedCell *)currentObject;
                break;
            }
        }
}
return cell;
11/06/2014 को 07:38
का स्रोत उपयोगकर्ता

वोट
0

: यहाँ है कि के लिए मेरे विधि है XIB फ़ाइलें से लोड हो रहा है कस्टम UITableViewCells ... अभी तक एक और तरीका

विचार का एक SampleCell उपवर्ग बनाने के लिए है UITableViewCellएक साथ IBOutlet UIView *contentसंपत्ति और प्रत्येक कस्टम subview आप कोड से विन्यस्त करने की जरूरत के लिए एक संपत्ति। फिर एक SampleCell.xib फ़ाइल बनाने के लिए। इस निब फ़ाइल में SampleCell करने के लिए फ़ाइल मालिक बदल जाते हैं। एक सामग्री जोड़ें UIViewअपनी आवश्यकताओं फिट करने के लिए आकार। जोड़ें और सभी subviews (लेबल, छवि विचारों, बटन, आदि) आप चाहते हैं कॉन्फ़िगर करें। अंत में, सामग्री देख सकते हैं और फ़ाइल मालिक को subviews लिंक।

24/03/2011 को 12:51
का स्रोत उपयोगकर्ता

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more