我有UITableViewCell
與UILabel
和UISwitch
。默認情況下,所有UISwitch
設置爲關閉。UITableView單元正在再生
一旦我將打開開關,然後通過表滾動開關值被設置爲默認值再次,ieOff
下面是我所使用的代碼:
- (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;
}
我在這裏做錯了什麼嗎?
謝謝。
如果我不使用它,標籤有覆蓋 – spaleja
你有你的答案呢:)儘量把開關的狀態綁定到保留的對象,並且需要一個單元格時只是重複使用它。 –
你能告訴我怎麼做到這一點嗎? – spaleja