0

我目前正在制作一个自定义 UITableView 单元格,如下所示。自定义 UITableViewCell 位于我从另一个 ViewController 调用的它自己的 nib 文件中。(像这样)

// 注册视图控制器.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; 
}

它有四个文本字段,我想将可以输入的字符串的大小限制为五个。(到目前为止,我只尝试使用第一个文本字段,但它甚至没有输入 textField:shouldChangeCharactersInRange:replacementString: 委托方法(在调试应用程序时发现)这是我试图限制的部分的代码可以输入的字符数量。

// 注册视图控制器.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;
}

格式化为注册字段

我想我已经将我的错误限制在两件事之一。也许我没有正确设置界面生成器中的所有内容。界面构建器

或者它与委派有关......我对此有一个大致的了解,这就是为什么我认为问题可能在这里,但是对于如此复杂的文件结构,我不确定这是如何或是否正确。

任何帮助、解释、建议等将不胜感激。

4

2 回答 2

1

在某些时候,您需要为 textField 设置委托

由于您将委托方法放在 RegistrationViewController.m 中,因此您可以在添加单元格后立即设置委托 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

只要您从 LogInCustomCell.xib 返回 UITableViewCell 的子类,就可以使用如下内容:

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;
于 2011-05-10T23:02:33.677 回答
0

据我所知,您在 RegistrationViewController.m 中有委托方法

但是您指出 CustomRegCell 是委托,所以委托方法应该在那里

于 2011-05-10T22:36:51.210 回答