【发布时间】:2013-12-10 17:09:09
【问题描述】:
我想知道是否有人遇到过我最近尝试过的类似问题。
让我进一步描述一下我的问题。我有一个 UITableViewController,我在 IB 中设计了一组自定义 UITableViewCell。每个 UITableViewCell 都有不同的元素,如 UIButton、UITextField 和 UILabel 等。显然,每个 UITableViewCell 都有不同的标识符。
一旦定义了我所有的 UITableViewCells,下一步就是在 tableView:cellForRowAtIndexPath: 上实例化单元格。我在这种方法中所做的取决于部分和行,我通过 dequeueReusableCellWithIdentifier:forIndexPath: 实例化不同的 UITableViewCells。
一切似乎都在桌子上完美且正确地创建,但是当我向下滚动时问题就来了。在最顶部,我有一个带有 UIButton 的 UITableViewCell,我已经指定了一个在单击它时要执行的具体操作。向下滚动,有几个 UITableViewCells 具有相同的格式(其中一个 UIButton 指定了不同的操作)。
问题是,当单击底部的第一个按钮时,该按钮执行我在最顶部 UIButton 上定义的第一个操作及其自己的操作。
似乎当 uitableviewdelegate 创建新单元格或重用它们时,它嵌套了其他 indexPath 的功能而不是指定的 indexPath....
希望我已经正确解释了自己。
提前谢谢你。
[编辑]
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [[UITableViewCell alloc] init];
NSLog(@"indexpath value is: %@", indexPath);
if (indexPath.section == 0)
cell = [self buildSection_0:tableView getIndexPath:indexPath];
else if (indexPath.section == 1)
cell = [self buildSection_1:tableView getIndexPath:indexPath];
else if (indexPath.section == 2)
cell = [self buildSection_2:tableView getIndexPath:indexPath];
else if (indexPath.section == 3)
cell = [self buildSection_3:tableView getIndexPath:indexPath];
else if (indexPath.section == 4)
cell = [self buildSection_4:tableView getIndexPath:indexPath];
return cell;
}
-(UITableViewCell *)buildSection_0:(UITableView *)tableView getIndexPath:(NSIndexPath *)indexPath{
NSString *identifier;
UITableViewCell *cell;
if (indexPath.row == 0){
identifier = @"headerCell";
cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
}
else if (indexPath.row == 1){
identifier = @"buttonCell";
cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
UIButton *button = (UIButton *)[cell viewWithTag:3001];
[button setTitle:@"Scan" forState:UIControlStateNormal];
[button addTarget:self action:@selector(scan) forControlEvents:UIControlEventTouchUpInside];
}
return cell;
}
-(UITableViewCell *)buildSection_3:(UITableView *)tableView getIndexPath:(NSIndexPath *)indexPath{
NSString *identifier;
UITableViewCell *cell;
if (indexPath.row == [[job jobLocations] count]) { // Adding button insert more Locations
identifier = @"buttonCell";
cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
UIButton *button = (UIButton *)[cell viewWithTag:3001];
[button setTitle:@"Add Location" forState:UIControlStateNormal];
[button addTarget:self action:@selector(newLocation) forControlEvents:UIControlEventTouchUpInside];
}
else{ // Showing different Locations
identifier = @"locationCell";
cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
UILabel *label = (UILabel *)[cell viewWithTag:3007];
UITextField *textField = (UITextField *)[cell viewWithTag:3008];
[label setText:[NSString stringWithFormat:@"Location #%ld", (long)indexPath.row + 1]];
[textField setText:[[[job jobLocations] objectAtIndex:indexPath.row] locationType]];
}
return cell;
}
【问题讨论】:
-
cellForRowAtIndexPath 的邮政编码
标签: ios uitableview