UIButton indexpath在UITableView中返回错误的路径



我正试图在正确填写的UITableView中访问按钮索引路径。我在anouther 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
}

我以前见过这个问题(但找不到引用),解决方法是不选择按钮的角(我认为只有当按钮位于单元格顶部时,这才是问题)。而不是

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];

请尝试使用此选项。我看到不少人抱怨这种方法表现得很奇怪。

CGPoint point = [self.tableView convertPoint:sender.frame.origin fromView:sender.superview]; 
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];

编辑

实现这一点的首选方法是让单元格创建按钮(可能在初始化过程中)。并为该单元格定义一个协议,该协议返回对该单元格的引用。将单元格设置为按钮代理,单击按钮时,将对单元格的引用传递回单元格代理,该代理可以调用

NSIndexPath *indexPath = [tableView indexPathForCell:cell];

例如

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];
        }
    }
}

为什么不将元素索引设置在UIButtonstagcellForRowAtIndexPath内?每次推送UIButton时,都会调用hatirlat,并且发件人可以访问索引。

也许这会因为你的两个UITableViews而引起问题,但我不知道你的确切代码。

- (void) hatirlat:(UIButton *)sender{
    UIButton *button = (UIButton *)sender;
    _arrayForPosition[button.tag];
}

相关内容

最新更新