无法识别的选择器发送到实例UITableViewCell

时间:2016-01-18 11:43:10

标签: ios objective-c xcode uitableview

我查了同样的问题,但给出的答案似乎并没有解决我的问题。

我正在创建一个应用程序,您可以在其中添加和删除学校标记。在我的视图控制器中,我创建了一个UITableView。在那里有一个名为UITableViewCell的自定义TableViewCell。要删除一个单元格我需要获取它的行。我通过在TableViewCell类中使用以下代码来尝试:

- (IBAction)DeleteMark:(id)sender {

   UITableView *superTableView = [self superview];
   NSIndexPath *path = [superTableView indexPathForCell:self];
   NSInteger *index = [path row];
   NSLog([NSString stringWithFormat:@"%@", index]);
}

当我运行代码时,它显示以下错误:

-[UITableViewWrapperView indexPathForCell:]: unrecognized selector sent to instance 0xa825a00

正如我之前所说,我尝试过在论坛上提供的解决方案,但没有一个能够奏效。

修改

这是cellForRowAtIndexPath

的代码
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

   TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];


   float weight = [[_editableClass.markWeights objectAtIndex:indexPath.row]floatValue];
   float mark = [[_editableClass.classMarks objectAtIndex:indexPath.row]floatValue];

   cell.MarkTextLabel.text = [NSString stringWithFormat:@"%.1f", mark];
   cell.MarkWeight.text = [NSString stringWithFormat:@"%.1f", weight];
   [cell.DeleteButton setTag:indexPath.row];
   return cell;
}

5 个答案:

答案 0 :(得分:0)

在你的UITableview Delegate方法的cellForRowAtIndexPath中设置你的按钮标签: -

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 .....
 cell.yourdeletebutton.tag=indexPath.row;
}

现在在你的删除方法中,它看起来像: -

- (IBAction)DeleteMark:(id)sender {

    UIButton *tappedButton = (UIButton*)sender;
    DeleteIndex=tappedButton.tag;
    NSLog(@"delete row at indexpath :%@", DeleteIndex]);
}

答案 1 :(得分:0)

您可以[sender tag]

使用
[cell.btnDelete setTag:indexPath.row];
[cell.btnDelete addTarget:self action:@selector(DeleteMark:) forControlEvents:UIControlEventTouchUpInside];

的方法,

- (IBAction) DeleteMark:(id)sender {

   NSInteger *index = [sender tag];
   NSLog([NSString stringWithFormat:@"%@", index]);

   // Your Code...

   //For Eg.
   Object *obj = [YOUR_ARRAY objectAtIndex:[sender tag]];
   // You'll get object of particular index.

   // Your Code...
}

感谢。

答案 2 :(得分:0)

您不应该依赖[[[[sender superview] superview] superview] superview],如果Apple决定进行一些更改并添加/删除另一层子视图,该怎么办? 带标签的想法也不理想。

我建议你使用委托模式或块。 请看看闭包是如何工作的。请注意,它是用swift编写的,但可以很容易地翻译成Objective-C。

TableViewCell添加处理程序属性中:

var buttonPressHandler: (() -> ())?

添加必须连接到按钮触摸事件的IBAction(您可以在故事板中执行此操作):

    @IBAction func buttonPressed(sender: AnyObject) {
        // Call handler when button is pressed
        buttonPressHandler?()
    }

cellForRowAtIndexPath中,您只需要设置处理程序:

    cell.buttonPressHandler = {
        print("Button pressed: \(indexPath.row)")
    }

答案 3 :(得分:0)

在尝试设置委托以传回单元格对象之后,执行类似于您的操作,以便从tableView中找到索引。我试着在下面做一个例子。为了整洁,我省略了import语句。

ViewController头文件

@interface subClassViewController : UIViewController<UITableViewDataSource, UITableViewDelegate,TableViewCellDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

ViewController实现文件

@implementation subClassViewController

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:    (NSIndexPath *)indexPath
{
    //setup cell...

    cell.delegate = self;
    return cell;
}

- (void)deletePressedOnCell:(UITableViewCell *)cell
{
    NSInteger index = [self.tableView indexPathForCell:cell].row;

    // do something with index
}

@end

TableViewCell头文件

@protocol TableViewCellDelegate

- (void)deletePressedOnCell:(UITableViewCell *)cell;

@end

@interface TableViewCell : UITableViewCell

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

@end

TableViewCell实现文件

@implementation TableViewCell

- (IBAction)DeleteMark:(id)sender
{
    [self.delegate deletePressedOnCell:self];
}

@end

答案 4 :(得分:-1)

使用以下代码获取行的索引路径

- (void)tapped:(UIButton *) sender {

   UITableViewCell * tCell;
   NSComparisonResult order = [[UIDevice currentDevice].systemVersion compare: @"7.2" options: NSNumericSearch];
   if (order == NSOrderedDescending) {
      tCell = (BusinessFavoriteCell *)[[[[sender superview] superview] superview] superview];
   } else {
      tCell = (BusinessFavoriteCell *)[[[sender superview] superview] superview];
   }
   NSIndexPath * iPath = [self.tableView indexPathForCell:tCell];
}

此处,tCell是以UITableViewCell方法加载的cellForRowIndexpath