我正在尝试在正确填充的UITableView中访问按钮索引路径。我在另一个XIB中定制的每个单元格中都有两个标签和一个按钮。当我单击任何部分中任何行中的按钮时,它返回错误的索引路径。我想知道是什么原因引起的。您可以在下面找到我的代码。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"ProgramCell";
ProgramCell *cell = (ProgramCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ProgramCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.contentView setUserInteractionEnabled: NO]; //
NSString* strTemp = [NSString stringWithFormat: @"%d/%d", indexPath.section, indexPath.row ];
for (int i = 0; i < buttonClickedArray.count; i++) {
if([[buttonClickedArray objectAtIndex:i] isEqualToString:strTemp])
{
[cell.btnHatirlat setBackgroundColor:[UIColor colorWithRed:0.0 green:0.5 blue:0. alpha:0.7]];
[cell.btnHatirlat setTitle:@"Eklendi" forState:UIControlStateNormal];
}
}
NSString *str = @"";
str = [arrayPazartesi objectAtIndex:indexPath.row];
if ([str length] > 1)
{
NSString *strSaat = [str substringToIndex:5];
NSString *strEtkinlikAdi = [str substringFromIndex:6];
cell.lblEtkinlik.text = strEtkinlikAdi;
cell.lblSaat.text = strSaat;
[cell.btnHatirlat addTarget:self action:@selector(hatirlat:)
forControlEvents:UIControlEventTouchUpInside ];
}
return cell;
}
- (void) hatirlat:(UIButton *)sender
{
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableProgram];
NSIndexPath *indexPath = [self.tableProgram indexPathForRowAtPoint:buttonPosition];
int j = indexPath.section; //true section
int i = indexPath.row; //wrong row
}
答案 0 :(得分:6)
之前我已经看过这个问题(但是找不到参考),修复就是不选择按钮的一角(我认为这只是一个问题,如果按钮位于顶部一个细胞)。而不是,
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableProgram];
尝试这样做,以确保你不是正确的,
CGPoint boundsCenter = CGRectOffset(sender.bounds, sender.frame.size.width/2, sender.frame.size.height/2).origin;
CGPoint buttonPosition = [sender convertPoint:boundsCenter toView:self.tableView];
答案 1 :(得分:0)
请尝试使用此功能。我见过很多人抱怨这种方法很奇怪。
CGPoint point = [self.tableView convertPoint:sender.frame.origin fromView:sender.superview];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];
修改强>
执行此操作的首选方法是让单元格创建按钮(可能在init期间)。并为此单元定义一个协议,该协议返回对该单元的引用。将单元格设置为按钮委托,单击该按钮时,将对单元格的引用传递回单元格委托,可以调用
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
e.g。
UITableViewCell.h
@protocol MyCellDelegate <NSObject>
@optional
- (void)cell:(UITableViewCell *)cell buttonPressed:(UIButton *)button;
@end
@interface MyCell : UITableViewCell
@property (weak, nonatomic) id<MyCellDelegate> delegate;
UITableViewCell.m
- (void)setDataWithDictionary:(NSDictionary *)dictionary
{
[self.titleLbl setText:[dictionary objectForKey:@"title"]];
[self.amountLbl setText:[dictionary objectForKey:@"amount"]];
btn = [[UIButton alloc] init];
[btn addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
.....
}
- (void)buttonPressed
{
if(self.delegate)
{
if([self.delegate respondsToSelector:@selector(cell:buttonPressed:)])
{
[self.delegate cell:self buttonPressed:btn];
}
}
}
答案 2 :(得分:0)
为什么不在UIButtons
内的tag
cellForRowAtIndexPath
内设置元素索引?
每次推送UIButton
hatirlat 时都会被调用,您可以通过发件人访问索引。
由于您的两个UITableViews
,可能会导致问题,但我不确切知道您的代码。
- (void) hatirlat:(UIButton *)sender{
UIButton *button = (UIButton *)sender;
_arrayForPosition[button.tag];
}