【发布时间】:2011-08-12 08:35:39
【问题描述】:
我有UITableViewCell、UILabel 和UISwitch。默认情况下,所有UISwitch 都设置为关闭。
一旦我打开开关,然后滚动表格,开关值将再次设置为默认值,即关闭
下面是我使用的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell != nil) cell = nil;
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if (indexPath.row == 0) {
UILabel *lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 100, 30)];
lbl1.text = @"Some Text";
[cell addSubview:lbl1];
switch = [[UISwitch alloc] initWithFrame:CGRectMake(190, 10, 200, 30)];
[switch setOn:NO];
switch.tag = 1;
[switch addTarget:self action:@selector(switchTapped:) forControlEvents:UIControlEventChanged];
[cell addSubview:switch];
}
}
// 下面是我的switchTapped方法:
- (void) switchTapped: (id)sender {
UISwitch *tapSwitch = (UISwitch *)sender;
switch (tapSwitch.tag) {
case 1:
if (tapSwitch.on) {
// do something
}
else {
// do something
}
break;
case 2:
if (tapSwitch.on) {
// do something
}
else {
// do something
}
break;
}
我在这里做错了吗?
谢谢。
【问题讨论】:
标签: iphone objective-c uitableview uiswitch