每个部分都有部分和不同的数组。它工作得很好,并显示正确的对象。但是当我将标签设置为UIBUtton时,indexPath.row作为button.tag传递,在第二个会话中返回1而不是0 ..
views.py
{
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
在这里我得到了indexPath
NSString *sectionTitle = [sectionsTitles objectAtIndex:indexPath.section];
NSArray *secAtt = [attractions objectForKey:sectionTitle];
Evento *evento = (Evento*)[secAtt objectAtIndex:indexPath.row];
UITableViewCell *cell = nil;
if(evento.listaImagens && [evento.listaImagens count] > 0) {
cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
}
else {
cell = [tableView dequeueReusableCellWithIdentifier:@"CellSemFoto" forIndexPath:indexPath];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
UIButton *btnCompartilhar = (UIButton *)[cell.contentView viewWithTag:40];
[btnCompartilhar.titleLabel setHidden:YES];
btnCompartilhar.titleLabel.text = sectionTitle;
btnCompartilhar.tag = indexPath.row;
[btnCompartilhar addTarget:self action:@selector(compartilharClick:) forControlEvents:UIControlEventTouchDown];
return cell;
}
索引在第二部分的第一行中返回1
编辑我发现indexpath.row是正确的但没有设置botaoCompartilhar.tag。
我用以下代码发现了它:
UIButton *botaoCompartilhar = (UIButton *)sender;
int index = botaoComapartilhar.tag;
答案 0 :(得分:0)
两个观察结果:
indexPath.row
,那么在选择器中您将有两个标记为1的按钮,两个标记为2,依此类推。UIControlEventTouchUpInside
而不是UIControlEventTouchDown
。总而言之,尝试做:
btnCompartilhar.tag = indexPath.section*1000 + indexPath.row;
而不是:
btnCompartilhar.tag = indexPath.row;
因此,您可以获得所有按钮的唯一标记值。然后你需要管理索引,如:
NSInteger indexSection = (button.tag)/1000;
NSInteger indexRow = (button.tag)%1000;
if (indexSection == 0){
//first section
//go get the element on index indexRow of the first section array
} else {
//second section
//go get the element on index indexRow of the second section array
}
答案 1 :(得分:0)
问题是我使用viewWithTag并将Button的标签更改为indexPath.row ...然后下一次,没有viewWithTag 40。
我的解决方案是
UIButton *btnCompartilhar = (UIButton *)[cell.contentView viewWithTag:40];
[btnCompartilhar.titleLabel setHidden:YES];
btnCompartilhar.titleLabel.text = [NSString stringWithFormat:@"%i|%@",indexPath.row, sectionTitle];
[btnCompartilhar addTarget:self action:@selector(compartilharClick:) forControlEvents:UIControlEventTouchDown];
并获取indexPath.row和sectionTitle:
int tag = 0;
NSString *titleLabel = botaoCompartilhar.titleLabel.text;
NSRange range = [botaoCompartilhar.titleLabel.text rangeOfString:@"|"];
if(range.location != NSNotFound) {
NSString *tagStr = [botaoCompartilhar.titleLabel.text substringToIndex:range.location];
tag = [tagStr intValue];
if([botaoCompartilhar.titleLabel.text length] > range.location+1)
titleLabel = [botaoCompartilhar.titleLabel.text substringFromIndex:range.location+1];
}