【问题标题】:How do you use custom UITableViewCell's effectively?您如何有效地使用自定义 UITableViewCell?
【发布时间】:2011-05-10 22:23:37
【问题描述】:

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

// 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: 委托方法(在调试应用程序时发现)这是我试图限制的部分的代码可以输入的字符数。

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

我认为我的错误仅限于两件事之一。 可能我没有在界面生成器中正确设置所有内容。

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

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

【问题讨论】:

  • 是否 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 被调用?
  • 嗯,不。从我读过的内容来看,每次在键盘上按下一个键时都应该调用它? (对吗?)但从我的调试来看,它永远不会被调用。

标签: iphone uitableview nsstring delegation custom-cell


【解决方案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;

【讨论】:

  • 啊...是的,我刚刚检查了我的 RegistrationViewController.nib 并意识到这是将委托传递给 tableview ..这就是为什么当我在自定义单元格的文本字段中输入时它没有调用该委托方法。所以这应该有希望工作会试一试并发布结果。谢谢。
  • 您仍然需要将 tableview 的委托设置为 RegistrationViewController.xib 中的文件所有者。
  • 并确保导入“LogInCustomCell.h”
  • 大声笑,我只为自定义单元格制作了 .nib 文件,然后将文件所有者链接到 RegistrationViewcontroller.m 类。这会改变什么吗?
  • 我将使用的方法是在 LogInCustomCell.h/.m 中将 UITableViewCell 子类化,并在 LogInCustomCell.xib 中将 UITableViewCell 的类设置为 LogInCustomCell。 nib 中的文件所有者无关紧要,因为您将自定义单元格从 nib 中拉出(我通常将其保留为 NSObject)。
【解决方案2】:

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

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

【讨论】:

  • Arcantos 说了什么,或查看similar question 了解更多实现目标的方法。
  • 我不确定这怎么可能,因为 CustomRegCell 只有一个 nib 文件。
  • Hrmm.. 你的例子没有多大帮助,因为它与我正在尝试做的事情非常相似,只是有问题。
  • 确保UITextFielddelegate 指向包含textField:shouldChangeCharactersInRange:replacementString: 方法的类。还要确保该类实现了UITextFieldDelegate 协议。
  • 我所做的是将registrationViewcontroller 类传递给customcell.nibs 文件所有者,然后定义我希望我的RegistrationViewcontroller 类符合的委托。这会达到同样的结果吗?
猜你喜欢
  • 1970-01-01
  • 2012-09-16
  • 2015-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-29
  • 1970-01-01
  • 2015-11-13
相关资源
最近更新 更多