UISwitch 出现在其他单元格上!毛刺?
Posted
技术标签:
【中文标题】UISwitch 出现在其他单元格上!毛刺?【英文标题】:UISwitch shows up on other cells! Glitch? 【发布时间】:2009-11-21 07:09:44 【问题描述】:我在一个表格中有 5 个单独的部分,其中 2 个部分具有可自定义的单元格,当处于编辑模式时,其中 2 个部分会生成一个附加单元格,该单元格会将一个新单元格添加到列表中。我的问题是,当我调用编辑模式时,UISwitch
出现故障!并决定跳来跳去,非常简单的代码,只是不知道为什么 UISwitch 在不应该跳转到不同部分的不同单元格时!
static NSInteger kCustomTextTag = 1;
static NSInteger kServiceTag = 3;
static NSInteger kConnectTag = 4;
static NSInteger kVibrateTag = 1;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Profile_ManagerAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
cell.hidesAccessoryWhenEditing = YES;
UISwitch *swService = nil;
swService = [[[UISwitch alloc] initWithFrame:CGRectMake(195, 8, 160, 27)] autorelease];
[swService addTarget:self action:@selector(serviceSwAction:) forControlEvents:UIControlEventValueChanged];
swService.tag = kServiceTag;
UISwitch *swConnect = nil;
swConnect = [[[UISwitch alloc] initWithFrame:CGRectMake(195, 8, 160, 27)] autorelease];
[swConnect addTarget:self action:@selector(connectSwAction:) forControlEvents:UIControlEventValueChanged];
swConnect.tag = kConnectTag;
UISwitch *swVibrate = nil;
swVibrate = [[[UISwitch alloc] initWithFrame:CGRectMake(195, 8, 160, 27)] autorelease];
[swVibrate addTarget:self action:@selector(vibrateSwAction:) forControlEvents:UIControlEventValueChanged];
swVibrate.tag = kVibrateTag;
if(indexPath.section == 0)
//CGRectMake Method (x, y, width, height)
custProfileNameLabel = [[[UITextField alloc] initWithFrame:CGRectMake(10, 11, 290, 21)] autorelease];
custProfileNameLabel.tag = kCustomTextTag;
custProfileNameLabel.textAlignment = UITextAlignmentLeft;
[cell.contentView addSubview:custProfileNameLabel];
custProfileNameLabel.backgroundColor = [UIColor clearColor];
custProfileNameLabel.userInteractionEnabled = NO;
custProfileNameLabel.placeholder = @"Custom Profile Name";
custProfileNameLabel.autocapitalizationType = UITextAutocapitalizationTypeWords;
custProfileNameLabel.clearButtonMode = UITextFieldViewModeWhileEditing;
//UIKeyboardTypeDefault;
cell.selectionStyle = UITableViewCellEditingStyleNone;
CustomProfile *cProf = [CustomProfile alloc];
cell.textLabel.text = [cProf tName];
if(indexPath.section == 1)
if(indexPath.row == ([[appDelegate serviceArray] count]) && self.editing)
cell.textLabel.text = @"Add a Service";
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
swService.hidden = YES;
return cell;
else
swService.hidden = NO;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
cell.textLabel.text = [[appDelegate serviceArray] objectAtIndex:indexPath.row];
[cell addSubview:swService];
if(indexPath.section == 2)
if(indexPath.row == ([[appDelegate connectArray] count]) && self.editing)
cell.textLabel.text = @"Add a Connection";
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
swConnect.hidden = YES;
return cell;
else
swConnect.hidden = NO;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
cell.textLabel.text = [[appDelegate connectArray] objectAtIndex:indexPath.row];
[cell addSubview:swConnect];
if(indexPath.section == 3)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *myPlistPath = [documentsDirectory stringByAppendingPathComponent:@"ProfileManager.plist"];
NSDictionary *plistDict = [[NSDictionary alloc] initWithContentsOfFile:myPlistPath];
NSString *value;
value = [plistDict objectForKey:@"Custom Ringtone"];
if(indexPath.row == ([[appDelegate ringArray] count]) && self.editing)
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
else
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = value;
if(indexPath.section == 4)
if(indexPath.row == 0 && self.editing)
//cell.userInteractionEnabled = NO;
cell.accessoryType = UITableViewCellAccessoryNone;
cell.textLabel.text = @"Vibrate";
swVibrate.hidden = YES;
else
swVibrate.hidden = NO;
//cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.textLabel.text = @"Vibrate";
[cell.contentView addSubview:swVibrate];
return cell;
截图
【问题讨论】:
这是另一部分故障的另一张图片:twitpic.com/qc3dx 【参考方案1】:好吧,这个故障之所以成为可能是因为控件是在创建单元格时创建的,所以它只是在视图出现时继续创建额外的控件。
如果您也收到此故障,请确保在 ViewDidLoad 中创建控件,或调用 void,只是不要在 cellForRowAtIndexPath 中创建控件:
【讨论】:
【参考方案2】:没有什么奇怪的事情发生。这是正常行为。注意这一行
UITableViewCell *cell = dequeueReusableCellWithIdentifier ...为了提高效率,tableview 控制器会保留以前使用过的单元格的队列,当您将 tableview 的新部分滚动到视图中时,它可能会返回以前使用过的单元格您可以重复使用(在这种情况下,返回的
cell
的值不会是 nil
)。如果该使用的单元格最初嵌入了一个 uiswitch,那么即使当前单元格不使用它,您也会再次获得该开关。
您需要检查您的单元分配代码是否返回给您的单元是现有的,如果它存在并且您不想要它,请移除开关。
霍华德
【讨论】:
以上是关于UISwitch 出现在其他单元格上!毛刺?的主要内容,如果未能解决你的问题,请参考以下文章