如何有效地使用自定义 UITableViewCell?

发布于 2024-11-06 03:46:42 字数 2973 浏览 4 评论 0原文

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

// 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; 
}

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

// 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

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

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

◇流星雨 2024-11-13 03:46:43

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

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

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

迟到的我 2024-11-13 03:46:42

在某些时候,您需要为 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;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文