0

I am currently making a custom UITableView cell as shown below. The custom UITableViewCell is in its own nib file that I am calling from another ViewController. (like so)

// RegistrationViewController.m

//Sets number of sections in the table
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

// Sets the number of rows in each section.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

//Loads both Custom cells into each section
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    //Registration Cell
    static NSString *CellIdentifier = @"CustomRegCell";
    static NSString *CellNib = @"LogInCustomCell";

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

    //Registration Button
    static NSString *CellButtonIdentifier = @"CustomSubmitCell";
    static NSString *CellButtonNib = @"LogInSubmitButton";

    UITableViewCell *cellButton = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellButtonIdentifier];
    if (cellButton == nil) {
        NSArray *nibButton = [[NSBundle mainBundle] loadNibNamed:CellButtonNib owner:self options:nil];
        cellButton = (UITableViewCell *)[nibButton objectAtIndex:0];
    }

    if (indexPath.section == 0) {
        cell.selectionStyle = UITableViewCellSelectionStyleNone; //Stops the UITableViewCell from being selectable
        [self registrationControll];
        //TODO: call method that controls this cell
        return cell;    
    }
    if (indexPath.section == 1) {
        cellButton.selectionStyle = UITableViewCellSelectionStyleNone; //Stops the UITableViewCell from being selectable
        return cellButton;          
    }
    return nil; 
}

It has four text fields that I am wanting to limit the size of the string that can be entered to five. (I'm only trying it with the first text field so far but its not even entering the textField:shouldChangeCharactersInRange:replacementString: delegate method (found this out while debugging the app) here is the code for the part I am trying to restrict the amount of characters that can be entered.

// RegistrationViewController.m

//textField:shouldChangeCharactersInRange:replacementString:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    int length = [regFieldOne.text length] ;
    if (length >= MAXLENGTH && ![string isEqualToString:@""]) {
        regFieldOne.text = [regFieldOne.text substringToIndex:MAXLENGTH];
        return NO;
    }
    return YES;
}

Formatted as a registration field

I think I have limited my error to one of two things. Maybe I haven't set everything up in interface builder correctly.Interfacebuilder

OR it has something to so with delegation... which I have a general understanding of and is why I think the issue might be here, but with such a complex file structure I'm not sure how or if this is right.

Any help, explanations, suggestions etc would be greatly appreciated.

tinhead
  • 385
  • 2
  • 10
  • 29
  • Is - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string getting called? – MrAnonymous May 10 '11 at 22:27
  • Hrmm, no. from what I have read its should be called every time a key is pressed on the key board? (is that right?) but from my debugging it never gets called. – tinhead May 10 '11 at 22:31

2 Answers2

1

At some point, you need to set the delegate for the textField

Since you put the delegate method in RegistrationViewController.m, you can set the delegate right after adding the cell in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath.

As long as you are returning a subclass of UITableViewCell from LogInCustomCell.xib, you can use something like this:

LogInCustomCell *cell = (LogInCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
    cell = (LogInCustomCell *)[nib objectAtIndex:0];
}
cell.textField1.delegate = self;
cell.textField2.delegate = self;
cell.textField3.delegate = self;
cell.textField4.delegate = self;

...

return cell;
  • Ahh... yea I was just going over my RegistrationViewController.nib and realised that it was the passing the delegate to the tableview.. which is why when I was typing in the textfield of the custom cell it was not calling that delegate method. so this should hopefully work Will give it a try and post the results. Thanks. – tinhead May 10 '11 at 23:09
  • You will still need the tableview's delegate set to the File's Owner in RegistrationViewController.xib. – Christopher Moore May 10 '11 at 23:15
  • and also make sure to import "LogInCustomCell.h" – Christopher Moore May 10 '11 at 23:16
  • lol, I only made the .nib file for the custom cell then linked the fileowner to the RegistrationViewcontroller.m class. Dose that change anything? – tinhead May 10 '11 at 23:19
  • the method I would use is to also subclass UITableViewCell in LogInCustomCell.h/.m and in LogInCustomCell.xib set the UITableViewCell's class to LogInCustomCell. The File's Owner in the nib doesn't matter since you are pulling the custom cell out of the nib (I usually leave it as NSObject). – Christopher Moore May 10 '11 at 23:29
  • The structure or union message is often about a missing `*` in your variable definition. – Pål Brattberg May 10 '11 at 23:40
  • I'm also just woundering how to also subclass UITableViewCell. The only way I currently know how to subclass something is when setting up the file itself.. and have not really found an example where you subclass two things in one .h file.. – tinhead May 11 '11 at 00:36
  • I have now changed it so that I am subclassing the two UITableViewCells from my RegistrationViewController.xib file. and deleted the other two .xibs. I have them displaying now and I think all I have to do is set up the delegate to each text field... – tinhead May 11 '11 at 02:27
  • Try this out: http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7-SW18 – Christopher Moore May 12 '11 at 13:40
0

From what I can see You have the delegate methods in RegistrationViewController.m

But you are idnicating that CustomRegCell is the delegate, so the delegate methods should be there

Arcantos
  • 1,052
  • 1
  • 13
  • 28
  • I'm not sure how thats possible as the CustomRegCell only has a nib file. – tinhead May 10 '11 at 22:39
  • I suggest creating a view controller for it – Arcantos May 11 '11 at 18:30
  • What Arcantos said, or see this [similar question](http://stackoverflow.com/questions/388237/getting-the-value-of-a-uitextfield-as-keystrokes-are-entered) for more ways to accomplish your goals. – Pål Brattberg May 10 '11 at 22:38
  • Hrmm.. you example didn't help much as it is pretty much similar to what I am trying to do, only with problems. – tinhead May 10 '11 at 22:45
  • Make sure the `delegate` of the `UITextField` points at the class with your `textField:shouldChangeCharactersInRange:replacementString:` method in it. Also make sure that class implements the `UITextFieldDelegate` protocol. – Pål Brattberg May 10 '11 at 23:18
  • What I have done is passed the customcell.nibs fileowner the RegistrationViewcontroller class, then defined the delegates I want my RegistrationViewcontroller class to conform to. will that achieve the same outcome? – tinhead May 10 '11 at 23:31
  • No, you also need to drag a connection from the `UITextField` to the `RegistrationViewController` as `delegate`for the `UITextField`. If you don't, you will not get that method called (which I'm assuming is still the case?). – Pål Brattberg May 10 '11 at 23:38