處理自定義UITableViewCell
按鈕的按鈕觸摸事件的最佳做法是什麼?Objective c - 處理自定義UITableViewCell按鈕的按鈕觸摸事件的最佳做法
我的課: MyViewController
,MyCustomCell
我能想到的三個選項:
第一選項 -有按鈕的MyCustomCell
的屬性,然後在MyViewController
添加一個目標吧.m文件以MyViewController
作爲目標。
MyViewController
.m文件
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"customCell";
MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
[cell.theButton addTarget:self
action:@selector(theButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];
}
// Configure the cell...
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)theButtonTapped:(UIButton *)sender
{
MyCustomCell *selectedCell = (MyCustomCell *)sender.superview;
if (selectedCell) {
NSIndexPath *indexPath = [self.tableView indexPathForCell:selectedCell];
MyModel *selectedModel = [self.model objectAtIndex:indexPath.row];
// do something with the model...
}
}
第二選項 -如果自定義單元格在IB做,將筆尖文件的所有者是MyViewController
,在MyViewController
實施buttonTapped:
方法和連接按鈕的觸摸的內心事件到buttonTapped:
方法。
第三選項 -如果自定義單元格並沒有在IB做,在MyCustomCell
.m文件添加一個目標按鈕與MyCustomCell
爲目標。
定義MyCustomCellDelegate
將@property (nonatomic, assign) id<MyCustomCellDelegate> delegate
添加到MyCustomCell
,並在點擊按鈕時調用此代理。
設置MyViewController
作爲單元的委託時創建單元格和實現MyCustomCellDelegate
協議。
MyCustomCell
.h文件中
@class MyCustomCell;
@protocol MyCustomCellDelegate <NSObject>
- (void)buttonTappedOnCell:(MyCustomCell *)cell;
@end
@interface MyCustomCell : UITableViewCell
@property (nonatomic, retain) UIButton *theButton;
@property (nonatomic, assign) id<MyCustomCellDelegate> delegate;
@end
MyCustomCell
.m文件
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.theButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.theButton.frame = CGRectMake(10,10,50,30);
[self addSubview:self.theButton];
[self.theButton addTarget:self
action:@selector(theButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
- (void)theButtonTapped:(UIButton *)sender
{
[self.delegate buttonTappedOnCell:self];
}
MyViewController
.m文件
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"customCell";
MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.delegate = self;
}
// Configure the cell...
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)buttonTappedOnCell:(MyCustomCell *)selectedCell
{
if (selectedCell) {
NSIndexPath *indexPath = [self.tableView indexPathForCell:selectedCell];
MyModel *selectedModel = [self.model objectAtIndex:indexPath.row];
// do something with the model...
}
}
我會去的選項1似乎最可讀的我。我使用了類似的方法,但是我在自定義單元格中設置了委託並處理自定義單元格中的按鈕,然後通過檢查委託是否在那裏調用視圖控制器中的函數。 –