我一直在這個問題上我的腦子一天左右現在...我已經搜索這個論壇,以及谷歌和其他博客等所有沒有無濟於事。UITableView與GUI元素UIButton,UISwitch等多個部分
我在做什麼是創建具有多個部分,並且每個部分內UITableView
,每一行都有不同的GUI元素(即設置屏幕)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
和所有我期待實現的每個部分在各個單元中具有各種GUI元素。但你可以在這個截圖中看到:
起初一切都顯得理所應當,但向上和向下滾動一次之後,問題開始。元素開始隨機拖動,並且越往上或向下滾動,元素移動得越多。
![ 「的UITableView在部分GUI元素和行。漏洞?毛刺?無知?」] [2]
這是一個錯誤?毛刺?或無知? (可能是後者) - 我已經除了GUI代碼以外的所有其他代碼,並且如果它意味着我正在使用NIB創建UITableView
的任何東西
另外,這裏是我的cellForRowAtIndexPath
方法的代碼。如果有人能指引我正確的方向或告訴我我做錯了什麼,我將永遠感激。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell
// desired section
if(indexPath.section == 0) {
//
// Load Settings
//
// button
UIButton * loadButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
loadButton.frame = CGRectMake(0.0, 0.0, 300.0, 44.0);
[loadButton setTitle:@"Load Image" forState:UIControlStateNormal];
[loadButton setTitle:@"Load Image" forState:UIControlStateSelected];
[loadButton addTarget:self action:@selector(loadImage:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:loadButton];
} else if(indexPath.section == 1) {
//
// Mode Settings
//
if(indexPath.row == 0) {
cell.text = [mode objectAtIndex:indexPath.row];
} else if(indexPath.row == 1) {
cell.text = [mode objectAtIndex:indexPath.row];
}
} else if(indexPath.section == 2) {
//
// Marker Settings
//
if(indexPath.row == 0) {
// description text
cell.text = [marker objectAtIndex:indexPath.row];
// switch
UISwitch *markerSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
cell.accessoryView = markerSwitch;
[markerSwitch setOn:YES animated:NO];
[markerSwitch addTarget:self action:@selector(markerToggle:) forControlEvents:UIControlEventValueChanged];
[markerSwitch release];
} else if(indexPath.row == 1) {
UISlider *markerGridSlider = [[UISlider alloc] initWithFrame:CGRectMake(0.0, 10.0, 284, 10.0)];
cell.accessoryView = markerGridSlider;
markerGridSlider.minimumValueImage = [UIImage imageNamed:@"size_icon_1x1.png"];
markerGridSlider.maximumValueImage = [UIImage imageNamed:@"size_icon_8x8.png"];
[markerGridSlider setMinimumValue:1.0];
[markerGridSlider setMaximumValue:8.0];
markerGridSlider.value = 8.0;
[markerGridSlider addTarget:self action:@selector(markerGrid:) forControlEvents:UIControlEventValueChanged];
[markerGridSlider release];
} else if(indexPath.row == 2) {
UISlider *markerSpacingSlider = [[UISlider alloc] initWithFrame:CGRectMake(0.0, 10.0, 284, 10.0)];
cell.accessoryView = markerSpacingSlider;
markerSpacingSlider.minimumValueImage = [UIImage imageNamed:@"spacing_icon_min.png"];
markerSpacingSlider.maximumValueImage = [UIImage imageNamed:@"spacing_icon_max.png"];
[markerSpacingSlider setMinimumValue:1.0];
[markerSpacingSlider setMaximumValue:(320.0/8.0)];
markerSpacingSlider.value = 1.0;
[markerSpacingSlider addTarget:self action:@selector(markerSpacing:) forControlEvents:UIControlEventValueChanged];
[markerSpacingSlider release];
}
}
return cell;
}
謝謝。
肯
感謝阿龍的幫助。或許我並不完全瞭解你的意思,但我認爲我已經正確實施了你的建議。不過,我仍然得到了交換位置的元素的小故障,雖然現在約5+的UP和DOWN擦除之後,我的UISwitch現在被放置在左上角。
這裏是我更新的代碼:
- (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];
if([indexPath section] == 0) {
//
// Load Settings
//
// button
UIButton *loadButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] autorelease];
loadButton.frame = CGRectMake(0.0, 0.0, 300.0, 44.0);
[loadButton addTarget:self action:@selector(loadImage:) forControlEvents:UIControlEventTouchUpInside];
[loadButton setTag:1000];
[cell.contentView addSubview:loadButton];
}
if([indexPath section] == 1) {
//
// Mode Settings
//
if([indexPath row] == 0) {
cell.text = [mode objectAtIndex:[indexPath row]];
}
if([indexPath row] == 1) {
cell.text = [mode objectAtIndex:[indexPath row]];
}
}
if([indexPath section] == 2) {
//
// Marker Settings
//
if([indexPath row] == 0) {
// switch
UISwitch *markerSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
[markerSwitch setOn:YES animated:NO];
[markerSwitch addTarget:self action:@selector(markerToggle:) forControlEvents:UIControlEventValueChanged];
[markerSwitch setTag:3000];
[cell.contentView addSubview:markerSwitch];
[markerSwitch release];
}
if([indexPath row] == 1) {
// slider
UISlider *markerGridSlider = [[UISlider alloc] initWithFrame:CGRectMake(0.0, 10.0, 284, 10.0)];
markerGridSlider.minimumValueImage = [UIImage imageNamed:@"size_icon_1x1.png"];
markerGridSlider.maximumValueImage = [UIImage imageNamed:@"size_icon_8x8.png"];
[markerGridSlider setMinimumValue:1.0];
[markerGridSlider setMaximumValue:8.0];
[markerGridSlider setValue:8.0];
[markerGridSlider addTarget:self action:@selector(markerGrid:) forControlEvents:UIControlEventValueChanged];
[markerGridSlider setTag:3100];
[cell.contentView addSubview:markerGridSlider];
[markerGridSlider release];
}
if([indexPath row] == 2) {
// slider
UISlider *markerSpacingSlider = [[UISlider alloc] initWithFrame:CGRectMake(0.0, 10.0, 284, 10.0)];
markerSpacingSlider.minimumValueImage = [UIImage imageNamed:@"spacing_icon_min.png"];
markerSpacingSlider.maximumValueImage = [UIImage imageNamed:@"spacing_icon_max.png"];
[markerSpacingSlider setMinimumValue:1.0];
[markerSpacingSlider setMaximumValue:(320.0/8.0)];
[markerSpacingSlider setValue:1.0];
[markerSpacingSlider addTarget:self action:@selector(markerSpacing:) forControlEvents:UIControlEventValueChanged];
[markerSpacingSlider setTag:3200];
[cell.contentView addSubview:markerSpacingSlider];
[markerSpacingSlider release];
}
}
} // end cell == nil
// Load
UIButton *loadButton = (UIButton*)[cell.contentView viewWithTag:1000];
[loadButton setTitle:@"Load Image" forState:UIControlStateNormal];
[loadButton setTitle:@"Load Image" forState:UIControlStateSelected];
// Mode
// Marker
UISwitch *markerSwitch = (UISwitch*)[cell.contentView viewWithTag:3000];
UISlider *markerGridSlider = (UISlider*)[cell.contentView viewWithTag:3100];
UISlider *markerSpacingSlider = (UISlider*)[cell.contentView viewWithTag:3200];
return cell;
}
好的。我可能不完全瞭解你的意思。儘管我認爲我已經實施了你的建議。它需要大約5+ UP和DOWN來恢復故障,但它仍然存在。 – frederickk
修改我的答案。查看包含的示例。 –