从其他类调用方法时iOS代码未运行

标签 ios uitableview uibutton delegates

我有一个自定义 UITableViewCell xib,上面有两个按钮。一个编辑按钮,旨在将该单元格的信息传递到新的 View Controller 中,这是我目前遇到的问题。我在自定义单元格中为 TableViewController 创建了一个属性,并将按钮连接到此代码:

-(IBAction) editItem {
    self.itemsList = [[TheItemsList alloc] init];
    [self.itemsList editItem];
    NSLog(@"EDIT");
}
在 TheItemsList 里面我有这个代码:
-(void)editItem {
    NSLog(@"EDITAGAIN");
    EditItem *detailViewController = [[EditItem alloc] initWithNibName:@"EditItem" bundle:nil];
    detailViewController.selectedCountry = self.selectedCountry;
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    detailViewController.therow = indexPath.row;
    //Pass the selected object to the new view controller.
    
    // Push the view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
}
控制台为每种方法打印我的 NSLog,但 View 不会推送。这段代码以前用在 TableView 的 didSelectForIndexPath 部分,所以我知道它正在推送的 View Controller 很好,我只是不知道这里发生了什么。
更新:
我让它有点工作,强调有点。
项目单元格.h
#import <UIKit/UIKit.h>
#import "TheItemsList.h"
@class TheItemsList;
@class ItemCell;
@protocol ItemCellDelegate <NSObject>
@required
-(void)editTheItem:(ItemCell *)theCell;
@end

@interface ItemCell : UITableViewCell
@property (assign) id <ItemCellDelegate> delegate;

@property (nonatomic, retain) IBOutlet UILabel *itemName;
@property (nonatomic, retain) IBOutlet UILabel *itemPrice;
@property (nonatomic, retain) IBOutlet UILabel *itemAisle;
@property (nonatomic, retain) IBOutlet UIImageView *thumbnail;
@property (nonatomic, retain) TheItemsList *itemsList;



@end
.m
#import "ItemCell.h"
#import "EditItem.h"
@implementation ItemCell
@synthesize itemName = _itemName;
@synthesize itemAisle = _itemAisle;
@synthesize itemPrice = _itemPrice;
@synthesize thumbnail = _thumbnail;
@synthesize itemsList, delegate;
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
-(IBAction) editItem {
    [self.delegate editTheItem:self];
}


@end
现在,在 TableView
。H
#import <UIKit/UIKit.h>
#import "ItemCell.h"
@class ItemCell;
@protocol ItemCellDelegate;
@interface TheItemsList : UITableViewController <ItemCellDelegate>
.m
-(void)editTheItem:(ItemCell *)theCell {
    NSLog(@"EDITAGAIN");
    EditItem *detailViewController = [[EditItem alloc] initWithNibName:@"EditItem" bundle:nil];
    detailViewController.selectedCountry = self.selectedCountry;
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    detailViewController.therow = indexPath.row;
    //Pass the selected object to the new view controller.
    
    // Push the view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
}
我遇到的是两个问题。
1,当按下按钮时,tableview中的第一项不做任何事情。
2,其他行将运行代码,但它只对第一行的数据执行。

最佳答案

您应该在 UITableViewCell 中使用委托(delegate)模式将按钮按下委托(delegate)回 UIViewController,其中包含使用单元格的 UITableView。

然后在 cellForRowAt:IndexPath 中检索单元格并将其委托(delegate)设置为 cell.delegate = self .在 View Controller 中,您实现委托(delegate)并处理操作。

在单元格中设置委托(delegate)变量(var delegate: MyCellDelegate?)并在 editItem 函数中调用委托(delegate),如 self.delegate?.editItem()

关于从其他类调用方法时iOS代码未运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48917685/

相关文章:

ios - 如何在 CollectionView 委托(delegate)方法之外实例化自定义类 UICollectionViewCell?

iphone - 如何在分离的 UITableViewCell 和另一个 View 之间执行 segue

ios - 更改突出显示的 UIButton BG 颜色,但保留层角半径?

iphone - UICollectionViewController 中加载数据的问题

ios - 如何快速将 UIViewController 添加到 UIScrollView 中?

swift - UILabel 的字体在 View 重新出现之前不会调整

iphone - 如何更新 UITableView 的屏幕外单元格

ios - 通过 UIButton 从 Appdelegate 导航到另一个 View

ios - UIButton 的 title 属性记录正确,但在将按钮添加为 subview 时未显示

ios - 如何获取 UICollectionViewCell 的矩形?