【发布时间】:2015-07-27 17:33:53
【问题描述】:
每个部分都有部分和不同的数组。它工作得很好,并显示了正确的对象。但是当我将标签设置为 UIBUtton 时,作为 button.tag 传递的 indexPath.row 在第二个会话中返回 1 而不是 0..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)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;
}
在这里我得到了 indexPath
UIButton *botaoCompartilhar = (UIButton *)sender;
int index = botaoComapartilhar.tag;
索引在第二部分的第一行返回 1
编辑 我发现 indexpath.row 是正确的,但没有设置 botaoCompartilhar.tag。
我是通过以下代码发现的:
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];
NSLog(@"%@ %i %i", evento.entretenimento.nome, btnCompartilhar.tag, indexPath.row);
【问题讨论】:
-
索引的用途是什么?
-
例如。第二部分的第二行返回 0 而不是 1...将是 1...有时索引是错误的...0 而不是 1. 1 而不是 0..1 而不是 2...只有第一部分总是正常工作
-
我的意思是
int index变量将用于什么? -
变量索引用于知道当用户点击分享按钮时应该从数组中取出哪个元素
标签: ios uitableview