के बटन के लिए बटन स्पर्श ईवेंट को संभालने का सर्वोत्तम अभ्यास कस्टम UITableViewCell
के बटन के लिए बटन स्पर्श ईवेंट को संभालने का सबसे अच्छा अभ्यास क्या है?उद्देश्य सी - कस्टम UITableViewCell
मेरी कक्षाएं: MyViewController
, MyCustomCell
मैं तीन विकल्प के बारे में सोच सकते हैं:
पहले विकल्पMyCustomCell
की एक संपत्ति के रूप में बटन है, और फिर MyViewController
में इसे करने के लिए एक लक्ष्य जोड़ने लक्ष्य के रूप में MyViewController
के साथ .m फ़ाइल।
MyViewController
मीटर फ़ाइल
- (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...
}
}
दूसरा विकल्प कस्टम सेल आईबी में बनाया गया था, तो, सेट निब फ़ाइल के मालिक MyViewController
होने की MyViewController
में buttonTapped:
विधि को लागू करने और कनेक्ट बटन के टच अप के अंदर घटना buttonTapped:
विधि के लिए।
तीसरा विकल्प यदि कस्टम सेल आईबी में नहीं बनाया गया था, एक लक्ष्य बटन को MyCustomCell
मीटर फ़ाइल में MyCustomCell
लक्ष्य के रूप में के साथ जोड़ें।
MyCustomCellDelegate
@property (nonatomic, assign) id<MyCustomCellDelegate> delegate
MyCustomCell
को परिभाषित करें और बटन को टैप करते समय इस प्रतिनिधि को कॉल करें।
कक्ष बनाने के दौरान सेल के प्रतिनिधि के रूप में MyViewController
सेट करें और MyCustomCellDelegate
प्रोटोकॉल को लागू करें।
MyCustomCell
ज फ़ाइल
@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
मीटर फ़ाइल
- (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
मीटर फ़ाइल
- (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. साथ जाना होगा यह मेरे लिए सबसे पठनीय लगता है। मैं एक समान दृष्टिकोण का उपयोग करता हूं, लेकिन मैंने कस्टम सेल में प्रतिनिधि सेट किया है और कस्टम सेल में बटन को संभाला है और फिर प्रतिनिधि नियंत्रक में एक फ़ंक्शन को कॉल करके यह जांच कर कॉल करें कि क्या प्रतिनिधि है। –