EddyBorja / MLPAutoCompleteTextField

UITextfield subclass with autocomplete menu. For iOS.
Other
1.21k stars 222 forks source link

Using a custom xib file for the TableViewCell #71

Open boazin opened 9 years ago

boazin commented 9 years ago

Is it supported? I'm having trouble with it...

gioy commented 9 years ago

hello, yes, using custom xib files for the tableViewCell(s) is possibile. Even if the demo won't build, you should take a look at the following MLPAutoCompleteTextField's methods:

in the MLPAutoCompleteTextField/MLPAutoCompleteTextField.m file

Also, take a look at the DEMOViewController and DEMOCustomAutoCompleteCell classes.

boazin commented 9 years ago

This one I did find, and here's exactly where I got complicated. Usually when I have a UITableView the cellForRowAtIndexPath has something like:

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

How do I incorporate this concept into registerAutoCompleteCellNib (it constantly kept crashing)

Thanks for the help - love the package

boazin commented 9 years ago

I'll elaborate. I have a text and an image. The image is downloaded async via SDWebImage It's size is unknown - and I want to "resize" it after download. Couldn't do it so far with the default UIImageView. A possible solution is a custom cell (As discussed in https://github.com/rs/SDWebImage/issues/30) But I got in trouble when trying to add my xib (with it's "attached" class)

A point to the direction (or a demo with xib) will be appreciated

gioy commented 9 years ago

If you need to use a custom UITableViewCell with the MLPAutoCompleteTextField, you just need to define which Cell Class and Cell xib you want to use. In your ViewController (where you're using the MLPAutoCompleteTextField instance), usually (but not necessarily) inside the - (void)viewDidLoad method, you just need to call the methods previously mentioned: (void)registerAutoCompleteCellClass:(Class)cellClass forCellReuseIdentifier:(NSString *)reuseIdentifier and (void)registerAutoCompleteCellNib:(UINib *)nib forCellReuseIdentifier:(NSString *)reuseIdentifier

Example:

for example's sake, let's suppose that:

@property (weak, nonatomic) IBOutlet MLPAutoCompleteTextField *searchField;

in your view controller's viewDidLoad method you'll have:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [_searchField registerClass:[MyCustomCell class]
           forCellReuseIdentifier:@"MyCustomCell"];

    [_searchField registerAutoCompleteCellNib:[UINib nibWithNibName:@"MyCustomCell" bundle:nil]
           forCellReuseIdentifier:@"MyCustomCell"];
}