2011-04-01 192 views
1

我創建了一個自定義單元格函數,其中有兩個uitextfields,因爲我不喜歡cell.textLabel和cell.detailTextLabel的外觀。當我爲cellForRowAtIndexPath中的表格創建單元格時,此函數被調用。當用戶選擇單元格時,會彈出一個鍵盤,以便您可以編輯第一個文本字段。但是,當您觸摸或接近第一個文本字段的文本時,它會拉起鍵盤,但不會觸發textFieldShouldReturn函數,如果在其他位置選擇了單元格,則會觸發該函數。我有textFieldShouldReturn函數將數據保存到應用程序。在單元格內選擇自定義UITextView時選擇單元格

這是自定義細胞功能

- (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; 

}

的的cellForRowAtIndexPath功能

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
UITableViewCell *cell; 
UITextField *tempValue; 
UITextField *descript; 

UIButton *primaryBtn; 

UIButton *secondaryBtn; 

cell = [self.tableView 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; 

}

我用willSelectRowAtIndexPath觸發我的應用程序的其餘部分,並作爲我只要標籤tempValue不是之前說的選中,一切正常。有沒有辦法,如果tempValue標籤被用戶按下,它將第一個響應者傳遞給單元格,以便如果適當地觸發我的應用程序的其餘部分?

+0

如果在Stack Overflow上已有某個答案,請將該信息轉發給我。 – user688341 2011-04-01 23:03:59

回答

1

我找到了答案,在發佈這個問題之前,我應該看起來更好。 This answer worked for me這是terra關於userinteractionenabled的答案。我所做的是在單元格創建時將userinteractionenabled設置爲NO,以便在我的文本框中標記tempValue,然後在willSelectRowForIndexPath中將userinteractionenabled切換爲YES。這很容易,我應該知道更好。