gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:39:23 25 4
gpt4 key购买 nike

我试图在正确填充的 UITableView 中到达按钮索引路径。我在另一个 XIB 中自定义的每个单元格中都有两个标签和一个按钮。当我单击任何部分中任何行中的按钮时,它返回 mw 错误的索引路径。我想知道是什么原因造成的。您可以在下面找到我的代码。

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

关于ios - UIButton indexpath 在 UITableView 中返回错误的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26342085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com