我试图使用几个不同的重用标识符来让用户能够在没有segue的情况下输入数据...(只是觉得它会很好)。
这是我先试过的:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if(addC && indexPath.row == 0 && indexPath.section == 0){
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
UITextField *txtField = [[UITexField alloc] initWithFrame:CGRectMake(10, 7, 260, 30);
[cell addsubview:txtField];
cell.txtField.delegate = self;
cell.txtField.text = nil;
return cell;
然而,我注意到如果我多次使用该字段,文本字段将复制,留下旧值。 经过一些研究后,我补充道:
if(cell == nil){
}
围绕txtField初始化但它不会触发。我最终通过创建一个单独的UITableViewCell类来解决这个问题,但现在我正在尝试添加 SWTableViewCell 并将我的单元格从UITableViewCell更改为CustomTableViewCell。
此自定义实用程序已添加到我的表中,初始化需要您使用
if (cell == nil) {
NSMutableArray *leftUtilityButtons = [NSMutableArray new];
NSMutableArray *rightUtilityButtons = [NSMutableArray new];
[leftUtilityButtons sw_addUtilityButtonWithColor:
[UIColor colorWithRed:0.07 green:0.75f blue:0.16f alpha:1.0]
icon:[UIImage imageNamed:@"check.png"]];
[leftUtilityButtons sw_addUtilityButtonWithColor:
[UIColor colorWithRed:1.0f green:1.0f blue:0.35f alpha:1.0]
icon:[UIImage imageNamed:@"clock.png"]];
[leftUtilityButtons sw_addUtilityButtonWithColor:
[UIColor colorWithRed:1.0f green:0.231f blue:0.188f alpha:1.0]
icon:[UIImage imageNamed:@"cross.png"]];
[leftUtilityButtons sw_addUtilityButtonWithColor:
[UIColor colorWithRed:0.55f green:0.27f blue:0.07f alpha:1.0]
icon:[UIImage imageNamed:@"list.png"]];
[rightUtilityButtons sw_addUtilityButtonWithColor:
[UIColor colorWithRed:0.78f green:0.78f blue:0.8f alpha:1.0]
title:@"More"];
[rightUtilityButtons sw_addUtilityButtonWithColor:
[UIColor colorWithRed:1.0f green:0.231f blue:0.188 alpha:1.0f]
title:@"Delete"];
cell = [[SWTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:cellIdentifier
containingTableView:_tableView // For row height and selection
leftUtilityButtons:leftUtilityButtons
rightUtilityButtons:rightUtilityButtons];
cell.delegate = self;
}
如果还没有实现这个插件,但在我研究我的问题之前,现在我还没找到任何东西。
我也试过
if (cell.contentView == nil)
if (cell.contentView.subViews == nil)
if (cell.subViews == nil)
但由于某种原因,它不会像它应该的那样第一次开火。我做错了什么?
答案 0 :(得分:1)
第一个问题的答案(为什么cell == nil
永远不会被调用):
dequeueReusableCellWithIdentifier:forIndexPath:
和dequeueReusableCellWithIdentifier:
的文档声明:
如果您为指定的标识符注册了一个类并且必须创建一个新的单元格,则此方法通过调用其initWithStyle:reuseIdentifier:方法来初始化该单元格。对于基于nib的单元格,此方法从提供的nib文件加载单元格对象。如果现有单元可用于重用,则此方法将调用单元的prepareForReuse方法。
这说的是,如果您使用registerNib:forCellReuseIdentifier:
或registerClass:forCellReuseIdentifier:
来注册重用标识符,那么您所做的就是你的单元格将为零,因为你从未手动实例化,永远不会有机会自己实例化。
有几种方法可以解决这个问题,但最好和最有效的方法是创建一个UITableViewCell
子类,并在你的子类中添加你想要的任何自定义子视图,如果你正在使用它,只需设置属性每次重复使用子视图。
所以基本上只需将所有自定义子视图添加和初始化代码移动到initWithStyle:reuseIdentifier:
。
然后,您的tableView:cellForRowAtIndexPath:
方法将被重写为:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(addC && indexPath.row == 0 && indexPath.section == 0)
{
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
cell.txtField.delegate = self;
cell.txtField.text = nil;
return cell;
}
}
答案 1 :(得分:0)
如果cell
为nil
,那么您从表格视图中找不到任何单元格。因此,无论如何,txtField
添加都无效。
你想要寻找的是获得一个已经有文本字段的单元格?因此cell
明确不为nil
。
大多数程序使用UITableViewCell
的自定义子类,以便子视图的生命周期自然地与单元格本身匹配。在你的情况下:
cell.contentView
,而不是cell
; [cell.contentView.subviews count]
来判断您是否添加了任何内容,因为内容视图的点与表视图单元格可能添加或删除的任何其他子视图不同操作的一部分。