问题描述
我正在使用最新的 SDK 和 XCode 4.2 开发 iOS 4 应用程序.
我有一个带有部分和自定义 UITableViewCell
的 UITableView
.每个单元格都有一个 UIButton,所有这些按钮都有相同的 UIControlEventTouchUpInside
目标.
这是我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{static NSString* cellIdentifier = @"CalendarCell";CalendarEventCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];如果(单元格 == 零){NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CalendarEventCell" owner:nil options:nil];for(id currentObject in topLevelObjects){if ([currentObject isKindOfClass:[CalendarEventCell 类]]){单元格 = (CalendarEventCell *)currentObject;[cell.addToCalendarButton addTarget:self action:@selector(addEventToiCal) forControlEvents:UIControlEventTouchUpInside];休息;}}}...}
当用户触摸该按钮时,我如何知道点击的单元格位于哪个部分和行?
在按钮上放置一个标签.例如:
CalendarEventCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];如果(单元格 == 零){NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CalendarEventCell" owner:nil options:nil];for(id currentObject in topLevelObjects){if ([currentObject isKindOfClass:[CalendarEventCell 类]]){单元格 = (CalendarEventCell *)currentObject;[cell.addToCalendarButton addTarget:self action:@selector(addEventToiCal) forControlEvents:UIControlEventTouchUpInside];休息;}}}cell.addToCalendarButton.tag = ((indexPath.section & 0xFFFF) <
您需要将选择器更改为 @selector(addEventToiCal:)
并将方法更新为 -(void) addEventToiCal:(UIButton *)sender
.>
然后您可以向 -addEventToiCal:
if (!([sender isKindOfClass:[UIButton class]]))返回;NSUInteger 部分 = ((sender.tag >> 16) & 0xFFFF);NSUInteger 行 = (sender.tag & 0xFFFF);NSLog(@"第 %i 行第 %i 节中的按钮被按下.", section, row);
I'm developing an iOS 4 application with latest SDK and XCode 4.2.
I have a UITableView
with sections and with custom UITableViewCell
. Every cell has a UIButton and all of these buttons has the same target for UIControlEventTouchUpInside
.
This is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* cellIdentifier = @"CalendarCell";
CalendarEventCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CalendarEventCell" owner:nil options:nil];
for(id currentObject in topLevelObjects)
{
if ([currentObject isKindOfClass:[CalendarEventCell class]])
{
cell = (CalendarEventCell *)currentObject;
[cell.addToCalendarButton addTarget:self action:@selector(addEventToiCal) forControlEvents:UIControlEventTouchUpInside];
break;
}
}
}
...
}
When user touch inside that button, how can I know on which section and row was the cell that has been clicked?
Place a tag on the button. For example:
CalendarEventCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CalendarEventCell" owner:nil options:nil];
for(id currentObject in topLevelObjects)
{
if ([currentObject isKindOfClass:[CalendarEventCell class]])
{
cell = (CalendarEventCell *)currentObject;
[cell.addToCalendarButton addTarget:self action:@selector(addEventToiCal) forControlEvents:UIControlEventTouchUpInside];
break;
}
}
}
cell.addToCalendarButton.tag = ((indexPath.section & 0xFFFF) << 16) |
(indexPath.row & 0xFFFF);
You will need to change your selector to @selector(addEventToiCal:)
and update the method to -(void) addEventToiCal:(UIButton *)sender
.
You can then add something like the following to -addEventToiCal:
if (!([sender isKindOfClass:[UIButton class]]))
return;
NSUInteger section = ((sender.tag >> 16) & 0xFFFF);
NSUInteger row = (sender.tag & 0xFFFF);
NSLog(@"Button in section %i on row %i was pressed.", section, row);
这篇关于带有 UIButton 的自定义 UITableViewCell:点击了哪个按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!