0

我有一个UITableView自定义tableview单元格。我Cell的一个有一个UITextField,我正在处理自定义类textfield中的委托方法。tableviewCell一旦用户输入文本,我需要重新加载tableview,我做了以下事情但没有奏效,任何想法请帮助我。

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    UITableView *parentTable = (UITableView *)self.superview;
    [parentTable reloadData];
}
4

10 回答 10

3

注册视图控制器以接收数据已更改的通知,并在收到通知时刷新表。然后让解析器将其发送出去。

注册它很容易:

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(reloadTableView:)
                                             name:@"reloadTableView"
                                           object:nil];

您的刷新方法需要设置为接收这些通知,如下所示:

- (void)reloadTableView:(NSNotification *)notif {
    [self.yourTableName reloadData];
}

在 ViewDidUnload 中停止观察很重要:

[[NSNotificationCenter defaultCenter] removeObserver:self];

然后在解析器中,您需要在完成后简单地添加它:

[[NSNotificationCenter defaultCenter] postNotificationName:@"reloadTableView" 
                                                    object:nil];

视图控制器(以及使用该名称观察通知的任何其他人)将获取消息并执行其任务。

谢谢..

于 2013-06-27T05:58:58.190 回答
1

自定义单元格.h

@property (nonatomic, copy) void(^tapHandler)(NSUInteger tag);

CustomCell.m

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    NSUInteger tag = 10; //Need to change the tag
    self.tapHandler(10);
}

控制器.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
 {
    // Whatever content was previously there. Add the below line in addition

     cell.tapHandler = ^(NSUInteger tag){
        [tableView reloadData];
    };

    return cell
 }

希望能帮助到你!

于 2013-06-27T06:06:45.630 回答
0

Don't reload whole table view for that small operation. Just reload the Cell in which the text field is. Also didnot forget to update your data source as table view cell fetch the data from the data source while the cell is loading.

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
     [tableViewDataSource addObject:textField.text];//this will fill the data source with data of textfield
    CustomCell *cell = (CustomCell *)textField.superview.superview;
    NSIndexPath *indexPath = [parentTable indexPathForCell:cell];
    [parentTable reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
于 2013-06-27T06:25:11.677 回答
0

(UITableView *)self.superview;你不会得到表格视图。试试下面的代码:

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
      YourCell *cell = (YourCell *)[[textField superview] superview]; // you will get cell
      UITableView *table = cell.superview; // you will get table view
      [table reloadData];
}
于 2013-06-27T06:13:35.317 回答
0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    tableReload++;
    NSString *CellIdentifier = [NSString stringWithFormat:@"cell %d%d",indexPath.row,tableReload];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

        cell.textLabel.text = @"Hello";


    }
    // Configure the cell.
    return cell;
}
于 2013-06-27T06:49:29.333 回答
0
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
UITableView *parentTable = (UITableView *)textField.superview.superview;
[parentTable reloadData];
}
于 2013-06-27T06:10:53.960 回答
0

首先,textfiled的superview不是UITableview,它是tableviewcell或tableviewcell.contentview(取决于你的代码)

然后,您只需要将 tableview 设置为视图控制器的成员或属性,

然后

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    [self.tableview reloadData];
}
于 2013-06-27T05:57:52.630 回答
0

尝试这个,

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    [self.parentTable reloadData];
}
于 2013-06-27T05:58:42.100 回答
0

最好保留表格视图的参考CustomCell

 // interface
 UITableView *tableView;

 // propery 
 @property (nonatomic)UITableView * tableView;

然后

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  //your stuff
  cell.tableView= tableView;
}
于 2013-06-27T05:59:15.370 回答
0

尝试这个,

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
   [self.yourTableName reloadData];
   [self.yourTableName reloadInputViews];
}
于 2013-06-27T10:42:37.907 回答