开发者

I want a UItextField that is in UITableViewCell to become first responder only when the cell touched

开发者 https://www.devze.com 2023-03-18 13:42 出处:网络
This is what I\'m trying to achieve: I want a UITextField that is in UITableViewCell to become first responder only when the cell touched. I would like the text field to become the first responder on

This is what I'm trying to achieve:

I want a UITextField that is in UITableViewCell to become first responder only when the cell touched. I would like the text field to become the first responder only when I set it to first responder in the method:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath`

Any idea how I can achieve this? When the textfield is touched directly, tab开发者_JAVA技巧leView:didSelectRowAtIndexPath: is never called.

Thanks in advance.


I guess, u have a custom UITableViewCell. In that u could have a UITextField member. Right?

In the custom cell class, override the method,

- (void)setSelected:(BOOL)selected animated:(BOOL)animated:

In that , if selected == YES, then make the text field as first responder. Else , resign the first responder.


Let an object be the delegate of the UITextField and implement textFieldShouldBeginEditing:; return NO if the cell hasn't been selected. (You might be able to always return NO from this method, if calling becomeFirstResponder directly bypasses the check.)


For having the textField become the first responder when a uitableviewcell is selected, use your textField as your property, and call [self.textField1 becomeFirstResponder];

Note: You will need as many properties as your the number of UITableViewCells as each cell has a textField.

When a textField is touched, the compiler will not know that the corresponding row got selected. For this you will need to use tags, such as textField.tag == 0 for the first textField, textField.tag ==1 for the second TextField and so on. And in the textFieldDidBeginEditing you should check for the tag, and then link that value with the corresponding row selected.

Did any of these make sense ? :)


First, you need a custom table cell, your own subclass of UITableViewCell. In that implementation, you need to implement hitTest: to determine where the touch occurred. In that method you can determine if the touch was in fact inside the rect of your UITextField, and if it was, make it the first responder. Here's an example from some code I wrote for a project:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    if (self.editing) {
        if ([nickname pointInside:[self convertPoint:point toView:nickname] withEvent:nil])
            return [nickname hitTest:[self convertPoint:point toView:nickname] withEvent:event];

        return [super hitTest:point withEvent:event];
    }

    return [self contentView];
}

The attribute nickname, in this case, was a UITextField inside the custom UITableViewCell.

The condition around self.editing may or may not be relevant to your application. The idea here is show you how hitTest: might be used, in general.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号