Iphone 带动态UI开关的TableView-如何识别开关
我找了很多问题,但还是没能解决。 我试图动态地向TableView添加一些UI开关,并根据它们的状态更改一些数据。 使用以下代码添加开关对我来说效果很好:Iphone 带动态UI开关的TableView-如何识别开关,iphone,objective-c,cocoa-touch,Iphone,Objective C,Cocoa Touch,我找了很多问题,但还是没能解决。 我试图动态地向TableView添加一些UI开关,并根据它们的状态更改一些数据。 使用以下代码添加开关对我来说效果很好: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
UISwitch *aSwitch = [[[UISwitch alloc] init] autorelease];
[aSwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
[cell.contentView addSubview:aSwitch];
return cell;
}
我的问题是-(void)switchChanged:(id)发送者。如何识别已更改的开关以修改正确的相应数据
UISwitch *switch = (UISwitch *)sender;
UIView *contentView = [switch superview];
UITableViewCell *cell = (UITableViewCell *)[contentView superview];
NSIndexPath *indexPath = [[self tableView] indexPathForCell:cell];
这将为您提供索引XPath。如果您有一个数据源,还可以设置aSwitch.tag=indexPath.row
这将为您提供索引XPath。如果您有一个数据源,还可以设置
aSwitch.tag=indexPath.row
。首先您应该更改方法,以便正确重用单元格。你想用这样的东西
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UISwitch *aSwitch = [[[UISwitch alloc] init] autorelease];
aSwitch.tag = 23;
[aSwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
[cell.contentView addSubview:aSwitch];
}
UISwitch *aSwitch = [cell.contentView viewWithTag:23];
aSwitch.on = whatever;
// Configure the cell...
return cell;
}
然后,您可以获得更改开关的indexpath,如下所示:
- (void) switchChanged:(id)sender {
UISwitch *switch = (UISwitch *)sender;
UIView *contentView = [switch superview];
UITableViewCell *cell = (UITableViewCell *)[contentView superview];
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
// do something
}
首先,您应该更改方法,以便正确地重用单元格。你想用这样的东西
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UISwitch *aSwitch = [[[UISwitch alloc] init] autorelease];
aSwitch.tag = 23;
[aSwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
[cell.contentView addSubview:aSwitch];
}
UISwitch *aSwitch = [cell.contentView viewWithTag:23];
aSwitch.on = whatever;
// Configure the cell...
return cell;
}
然后,您可以获得更改开关的indexpath,如下所示:
- (void) switchChanged:(id)sender {
UISwitch *switch = (UISwitch *)sender;
UIView *contentView = [switch superview];
UITableViewCell *cell = (UITableViewCell *)[contentView superview];
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
// do something
}
使用UIView的标记属性(UISwitch继承自UIView):
这个案子正是这个酒店设计要解决的问题。动态创建开关时,只需增加一个静态变量
然后在-(void)switchChanged:(id)sender
方法中,您只需检查标记的值即可 使用UIView的标记属性(UISwitch继承自UIView):
这个案子正是这个酒店设计要解决的问题。动态创建开关时,只需增加一个静态变量
然后在
-(void)switchChanged:(id)sender
方法中,您只需检查标记的值即可 与这个问题无关,因为它已经得到了全面的回答,但您第一句中的代码样式文本段是因为您使用了反勾号(`)作为撇号。在StackOverflow上,您可以将文本的一部分括在后面的记号中,以获得代码样式,而不需要分段。对于普通文本,请坚持使用撇号(')。与问题无关,因为它已经得到了全面的回答,但第一句中的代码样式文本段是因为您使用了回勾字符(`)作为撇号。在StackOverflow上,您可以将文本的一部分括在后面的记号中,以获得代码样式,而不需要分段。对于普通文本,请使用撇号(')。这正是我要搜索的内容。很多坦克!没问题-很高兴我能帮忙!:)递增静态变量?这只有在他使用tableView:cellforrowatinexpath:
时才有效。实际上,我认为如果使用tableView:cellforrowatinexpath:
,这可能不起作用,因为您可能会将单元格出列,在这种情况下,标记可能不正确。应用程序的设计需要弄清楚如何识别控件。我认为这里的问题是如何识别控制。这正是我要寻找的。很多坦克!没问题-很高兴我能帮忙!:)递增静态变量?这只有在他使用tableView:cellforrowatinexpath:
时才有效。实际上,我认为如果使用tableView:cellforrowatinexpath:
,这可能不起作用,因为您可能会将单元格出列,在这种情况下,标记可能不正确。应用程序的设计需要弄清楚如何识别控件。我认为这里的问题是如何确定控制措施。