0

我有一个简单的问题:我无法"[tableView reloadData]"从.mUIButton中调用。UITableViewCell

我有一个tableView显示每行UITableViewCell包含一个的。UIButton当我单击单元格的按钮时,我想从我的 tableView 中重新加载数据。

4

2 回答 2

0

只要您持有对 tableView 的引用,您就应该能够通过点击按钮重新加载数据。最简单的方法是在头文件中进行引用

@interface MyClass ... {
    UITableView *myTableView;
    // all your other stuff;
}
// any methods and properties you want to declare;
@end

然后,当您将按钮放入- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法中的单元格时,请执行以下操作

UIButton *myButton = [UIButton buttonWithType:whateverTypeYouPick];
[myButton addTarget:self action:@selector(reloadTableView) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:myButton];  // or cell.contentView or wherever you want to place it

然后只需设置您的操作方法

- (IBAction)reloadTableView {
    [myTableView reloadData];
    // anything else you would like to do;
}

我对此进行了测试,对我来说效果很好,所以希望它也对你有用

于 2011-05-17T22:06:20.640 回答
0

一种方法是在 Cell 上设置委托,并在操作发生时让 tableViewController 实现委托。

MyCell.h

@protocol MyCellDelegate

-(void)myCell:(MyCell*)cell reloadTableView:(id)sender;

@end

@interface MyCell : UITableViewCell

@property (nonatomic, weak) id <MyCellDelegate> delegate;

-(IBAction)reloadTableView:(id)sender;

@end

我的细胞

@implementation MyCell

@property (nonatomic, weak) id <MyCellDelegate> delegate;

-(IBAction)reloadTableView:(id)sender;
{
    if(self.delegate)
    {
        [self.delegate myCell:self reloadTableView:sender];
    }
}

@end

在 tableViewController 中实现委托方法并执行您想要执行的任务。

-(void)myCell:(MyCell*)cell reloadTableView:(id)sender;
{
     CGPoint location = [sender locationInView:self.tableView];
     NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
    //Here is the indexPath
    [self.tableView reloadData];
}
于 2015-11-26T10:21:09.007 回答