I created a custom cell function that has two uitextfields in it, because I didn't like how cell.textLabel and cell.detailTextLabel looked. This function gets called when I create the cells for the table in cellForRowAtIndexPath. When the cell is selected by the user a keyboard pops up so you are allowed to edit the first textfield. However, when you touch on or near the text of the first textfield it pulls up the keyboard but it doesn't fire off the textFieldShouldReturn function that I have that does fire off if the cell is selected somewhere else. I have the textFieldShouldReturn function save the data to the app.
This is the custom cell function
- (UITableViewCell *)getCellContentView:(NSString *)cellIdentifier
{ CGRect CellFrame = CGRectMake(0, 0, 300, 60);
CGRect Label1Frame = CGRectMake(10, 8, 1, 25); CGRect Label2Frame = CGRectMake(10, 34, 100, 13); CGRect Image1Frame = CGRectMake(265, 10, 30, 30); CGRect Image2Frame = CGRectMake(230, 10, 30, 30);UITextField *lblTemp;
UIButton *imgTemp;
UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];
//initialize label with tag 1
lblTemp = [[UITextField alloc] initWithFrame:Label1Frame];
lblTemp.tag = 1;
[cell.contentView addSubview:lblTemp];
[lblTemp sizeToFit];
[lblTemp release];
lblTemp = [[UITextField alloc] initWithFrame:Label2Frame];
lblTemp.tag = 2;
[cell.contentView addSubview:lblTemp];
[lblTemp release];
imgTemp = [[UIButton alloc] initWithFrame:Image1Frame];
imgTemp.tag = 3;
[cell.contentView addSubview:imgTemp];
[imgTemp release];
imgTemp = [[UIButton alloc] initWithFrame:Image2Frame];
imgTemp.tag = 4;
[cell.contentView addSubview:imgTemp];
[imgTemp release];
return cell;
}
The cellForRowAtIndexPath function
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
UITextField *tempValue;
UITextField *descript;
UIButton *primaryBtn;
UIButton *secondaryBtn;
cell = [self.tableView 开发者_Go百科dequeueReusableCellWithIdentifier:cellIdentity];
if ([valueFromServer isEqualToString:@"comments"])
{
cell = [self cellIsView:cellIdentity];
messageTextView = (UITextView *)[cell viewWithTag:5];
messageTextView.returnKeyType = UIReturnKeyDone;
}
if (cell == nil)
{
cell = [self getCellContentView:cellIdentity];
}
primaryBtn = (UIButton *)[cell viewWithTag:3];
secondaryBtn = (UIButton *)[cell viewWithTag:4];
tempValue = (UITextField *)[cell viewWithTag:1];
if ([valueFromServer isEqualToString:@""])
{
tempValue.textColor = [UIColor grayColor];
}
tempValue.textColor = [UIColor blackColor];
tempValue.font = [UIFont boldSystemFontOfSize:16];
tempValue.placeholder = @"Touch cell to edit";
tempValue.text = getServerData;
descript = (UITextField *)[cell viewWithTag:2];
descript.enabled = NO;
descript.textColor = [UIColor darkGrayColor];
descript.font = [UIFont boldSystemFontOfSize:10];
descript.text = getServerLabel;
[tempValue sizeToFit];
...
return cell;
}
I use willSelectRowAtIndexPath to trigger the rest of my application, and as I said before as long as the label tempValue is not selected, everything works. Is there a way, that if the tempValue label is pressed by the user, for it to pass the firstresponder to the cell so that if fires off the rest of of my app properly?
I found the answer, and I should have looked better before I posted this question. This answer worked for me it's the answer from terra about userinteractionenabled. What I did was when the cell is created set userinteractionenabled to NO for my textfield, which was labeled tempValue in the example code, and then switch userinteractionenabled to YES in willSelectRowForIndexPath. It was so easy I should have known better.
精彩评论