我需要在 UITableViewCell
中使用 UITextField
。这是使用 Parse 开源,我希望它发回一个标题。所以当它调用 cellForRowAtIndexPath
时,它需要做这样的事情。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UITextField *TextField = (UITextField *) [cell viewWithTag:284];
[TextField setReturnKeyType:UIReturnKeyDone];
[TextField addTarget:self
action:@selector(TitleTextFieldFinished:indexPath:object:)
forControlEvents:UIControlEventEditingDidEndOnExit];
TextField.text = [object objectForKey:@"Toppingname"];
return cell;
}
那么它可能会这样调用:
- (void)TitleTextField:(UITextField *)TextField:(NSIndexPath *)indexPath:(PFObject *)object
{
[TextField resignFirstResponder];
PFQuery *item = [PFQuery queryWithClassName:@"className"];
[item getObjectInBackgroundWithId:self.menuid
block:^(PFObject *title, NSError *error) {
NSString *newTitle = @"New title";
title[@"title"] = newTitle;
[title saveInBackground];
}];
}
如果有人能提供帮助那就太好了!
最佳答案
实现可以像下面这样
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellId" forIndexPath:indexPath];
// Configure the cell...
UITextField *textField = (UITextField *) [cell viewWithTag:284];
[textField setReturnKeyType:UIReturnKeyDone];
[textField addTarget:self
action:@selector(titleTextField:inIndexPath:forObject:)
forControlEvents:UIControlEventEditingDidEndOnExit];
textField.text = @"Sandeep";
return cell;
}
- (void)titleTextField:(UITextField *)textField inIndexPath:(NSIndexPath *)indexPath forObject:(PFObject *)object {
[textField resignFirstResponder];
}
但是您不会获得“indexPath”和“object”值,因为 sender(UITextField) 不知道应该发送哪个 indexPath 和哪个对象。
关于iOS:调用方法的 UITableViewCell 中的 UITextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37449160/