如何有效地使用自定义 UITableViewCell?
我目前正在制作一个自定义 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;
}
我想我已将错误限制为两件事之一。 也许我没有在界面生成器中正确设置所有内容。
或者它与委托有关......这我对此有一个大致的了解,这就是为什么我认为问题可能在这里,但对于如此复杂的文件结构,我不确定这是如何或是否正确。
任何帮助、解释、建议等将不胜感激。
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;
}
I think I have limited my error to one of two things.
Maybe I haven't set everything up in interface builder correctly.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所见
您在 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
在某些时候,您需要为 textField 设置委托,
因为您将委托方法放在 RegistrationViewController.m 中,所以您可以在添加单元格后立即设置委托
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
。只要您从 LogInCustomCell.xib 返回 UITableViewCell 的子类,您就可以使用如下内容:
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: