【发布时间】:2011-04-22 18:39:49
【问题描述】:
我的 UITableViewCell 有一个小问题。 每个自定义单元格都有一个 UISwitch 和一个 UIButton。当用户更改 UISwitch 的值时,我想将 button.hidden 属性设置为 YES。
我可以通过这个方法找到哪个 UISwitch 被点击了:
- (IBAction)closeHour:(id)sender {
UISwitch *senderSwitch = (UISwitch *)sender;
UITableViewCell *switchCell = (UITableViewCell *)[senderSwitch superview];
NSUInteger buttonRow = [[resas indexPathForCell:switchCell] row];
NSLog("%d", buttonRow);
}
这工作完美,但我不知道如何让 UIButton 位于同一索引 (indexPath.row) 以设置他的隐藏属性。我想过在每个 UIButton 中设置一个标签,但我对这些不太友好。
我的 Cell 是如何创建的,如果有人能告诉我我是否在这里做一些废话,那就太好了:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier1 = @"Cell1";
static NSString *cellIdentifier2 = @"Cell2";
if ([typeOfData objectAtIndex:indexPath.row] == @"hour") {
TimeCell *cell = (TimeCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier1];
if (cell == nil) {
cell = [[[TimeCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier1] autorelease];
UISwitch *isOpen = [[UISwitch alloc] initWithFrame:CGRectMake(300, 7, 0, 0)];
if ([self openOrCloseHour:[[[finalData objectAtIndex:indexPath.row] substringToIndex:2] intValue]])
isOpen.on = YES;
else {
isOpen.on = NO;
[isOpen addTarget:self action:@selector(closeHour:) forControlEvents:UIControlEventValueChanged];
[cell addSubview:isOpen];
[isOpen release];
UIButton *add = [UIButton buttonWithType:UIButtonTypeContactAdd];
add.frame = CGRectMake(400, 3, 170, 40);
[add addTarget:self action:@selector(addResa:) forControlEvents:UIControlEventTouchDown];
[cell addSubview:add];
}
}
[cell.time setText:[finalData objectAtIndex:indexPath.row]];
return cell;
}
else
{
ResaCell *cell = (ResaCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier2];
if (cell == nil) {
cell = [[[ResaCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier2] autorelease];
[cell.isConfirm setImage:[UIImage imageNamed:@"confirm.png"]];
[cell.nom setText:[finalData objectAtIndex:indexPath.row]];
[cell.prenom setText:[finalData objectAtIndex:indexPath.row]];
[cell.arrive setText:[finalData objectAtIndex:indexPath.row]];
}
return cell;
}
return nil;
}
有关信息,我有两种类型的单元格。问题出在 TimeCell 上。
有人有解决办法吗?
【问题讨论】:
-
你好。看看我最后的回复(stackoverflow.com/questions/3888771/…)。你的问题是一样的。但是您还需要使用 viewWithTag 获得一个视图:^^ Good Luck
-
感谢您的回答,但我做不到。该方法由 UISwitch 而不是 UIButton 调用,因此发送者是 UISwitch,无法识别 UIButton。也许我错了,反正我失败了。
标签: ios iphone uitableview uibutton uiswitch