我有一个UITextField用于UITableView的customCell。 在UITextField的TapGesture上,我需要在UITableViewCell的右侧显示复选标记。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
PreferenceCellWithTextBox *cell = (PreferenceCellWithTextBox *)[tableView dequeueReusableCellWithIdentifier:@"PreferenceCellIdentifier"];
if (cell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"PreferenceCellWithTextBox"
owner:nil
options:nil];
for (id currentObject in topLevelObjects)
{
if ([currentObject isKindOfClass:[UITableViewCell class]])
{
cell=(PreferenceCellWithTextBox *)currentObject;
break;
}
}
if([self.checkedIndexPath isEqual:indexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(forwardToDidSelect:)];
[cell.userIdTextField addGestureRecognizer:tap];
}
return cell;
}
在这里,我正在为Tap Gesture写作。它只被调用一次。不应该那样发生。每当我触摸UITextField上的UITextField时,它就会被调用。
-(void)forwardToDidSelect:(UITapGestureRecognizer *)tap
{
[self tableView:self.userIdSuggestiontableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
}
在这里,我编写了选择行的代码。在选择任何行时,我想显示Checkmark。并删除上一行的复选标记。
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(self.checkedIndexPath)
{
PreferenceCellWithTextBox* uncheckCell =(PreferenceCellWithTextBox *) [tableView
cellForRowAtIndexPath:self.checkedIndexPath];
uncheckCell.accessoryType = UITableViewCellAccessoryNone;
}
if([self.checkedIndexPath isEqual:indexPath])
{
self.checkedIndexPath = nil;
}
else
{
PreferenceCellWithTextBox* cell = (PreferenceCellWithTextBox*)[tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.checkedIndexPath = indexPath;
}
}