我有一个定制的UITableViewCell xib,上面有两个按钮。一个编辑按钮,旨在将该单元格的信息传递到一个新的视图控制器中,这就是我目前遇到的问题。我在自定义单元格中为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,但视图不推送。这段代码以前是在表视图的didSelectForIndexPath部分中使用的,所以我知道它推送的视图控制器是正常的,我只是不知道这里发生了什么。
更新:
我让它在某种程度上起作用了,重点是有点。
ItemCell.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,当按钮被按下时,表视图中的第一项不执行任何操作。
2,其他行将运行代码,但它只对第一行的数据执行此操作。
发布于 2018-02-22 10:32:47
您应该在UITableViewCell中使用委托模式来委托按钮,按回包含使用单元格的UITableView的UIViewController。
在cellForRowAt:IndexPath中,您可以检索单元格并设置它的委托,如cell.delegate = self。在视图控制器中,实现委托并处理操作。
在单元格中设置一个委托变量(var委托: MyCellDelegate?)在editItem函数中,可以调用self.delegate?.editItem()这样的委托
发布于 2018-02-24 23:11:22
你的问题基本上是因为这一行
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];单击单元格上的按钮时,它不一定选择行。这个按钮需要触摸。解决这个问题的一种方法是找到按钮所在单元格的NSIndexPath,然后使用它执行操作。一种方法是使用从editAction委托提供的单元格引用
-(void)editTheItem:(ItemCell *)theCell用[self.tableView indexPathForCell:theCell]替换[self.tableView indexPathForSelectedRow]
https://stackoverflow.com/questions/48917685
复制相似问题