2013-06-27 70 views
0

我有UITableView自定义tableview单元格。我的其中一个CellUITextField,我正在处理tableviewCell类中的textfield委托方法。我需要重新加载tableview,一旦用户输入文本,我已经完成了下面的事情,但没有工作,任何想法请帮助我。无法从自定义单元格重新加载表格视图

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

你得到任何异常? –

+0

没有例外,没有重新加载.. – Vishnu

+0

我不认为你可以得到superview是单元格内的tableview!所以这段代码不起作用。尝试使用委托或观察者。这将解决我猜想的问题 – Meera

回答

0

第一,textfiled的上海华不UITableView的,它是tableviewcell或tableviewcell.contentview(取决于您的代码)

然后,你只需要设置的tableview为您的视图控制器的成员,或财产,

然后

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField 
{ 
    [self.tableview reloadData]; 
} 
0

试试这个,

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField 
{ 
    [self.parentTable reloadData]; 
} 
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]; 

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

谢谢..

0

这是更好地保持表格视图的参考CustomCell

// interface 
UITableView *tableView; 

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

然后

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    //your stuff 
    cell.tableView= tableView; 
} 
1

CustomCell.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); 
} 

Controller.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 
} 

希望它能帮助!

0
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField 
{ 
UITableView *parentTable = (UITableView *)textField.superview.superview; 
[parentTable reloadData]; 
} 
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]; 
} 
0

不要为整个小操作重新加载整个表视图。只需重新加载文本字段所在的单元格即可。也没有忘记更新您的数据源,因为表格视图单元在单元格加载时从数据源获取数据。

- (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]; 
} 
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; 
} 
+1

这显然是错误的,与问题无关。 – Groot

0

试试这个,

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField 
{ 
    [self.yourTableName reloadData]; 
    [self.yourTableName reloadInputViews]; 
} 
相关问题