How do you load custom UITableViewCells from Xib files?

前端 未结 23 1748
抹茶落季
抹茶落季 2020-11-22 11:11

The question is simple: How do you load custom UITableViewCell from Xib files? Doing so allows you to use Interface Builder to design your cells. The answer app

相关标签:
23条回答
  • 2020-11-22 11:39

    Here is my method for that: Loading Custom UITableViewCells from XIB Files… Yet Another Method

    The idea is to create a SampleCell subclass of the UITableViewCell with a IBOutlet UIView *content property and a property for each custom subview you need to configure from the code. Then to create a SampleCell.xib file. In this nib file, change the file owner to SampleCell. Add a content UIView sized to fit your needs. Add and configure all the subviews (label, image views, buttons, etc) you want. Finally, link the content view and the subviews to the file owner.

    0 讨论(0)
  • 2020-11-22 11:40

    I've decided to post since I don't like any of these answers -- things can always be more simple and this is by far the most concise way I've found.

    1. Build your Xib in Interface Builder as you like it

    • Set File's Owner to class NSObject
    • Add a UITableViewCell and set its class to MyTableViewCellSubclass -- if your IB crashes (happens in Xcode > 4 as of this writing), just use a UIView of do the interface in Xcode 4 if you still have it laying around
    • Layout your subviews inside this cell and attach your IBOutlet connections to your @interface in the .h or .m (.m is my preference)

    2. In your UIViewController or UITableViewController subclass

    @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. In your MyTableViewCellSubclass

    - (id) initWithCoder:(NSCoder *)aDecoder {
        if (self = [super initWithCoder:aDecoder]) {
            ...
        }
    
        return self;
    }
    
    0 讨论(0)
  • 2020-11-22 11:40

    If you're using Interface Builder to make cells, check that you've set the Identifier in the Inspector. Then check that it's the same when calling dequeueReusableCellWithIdentifier.

    I accidentally forgot to set some identifiers in a table-heavy project, and the performance change was like night and day.

    0 讨论(0)
  • 2020-11-22 11:41

    The right solution is this:

    - (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;
    }
    
    0 讨论(0)
  • 2020-11-22 11:44

    This extension requires 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
        }
    }
    

    Create an Xib file that contains just 1 custom UITableViewCell.

    Load it.

    let cell: BacteriaCell = try NSBundle.mainBundle().loadView("BacteriaCell")
    
    0 讨论(0)
提交回复
热议问题