我有一个UITableView
当我选择一个单元格时,我想在单元格的右边放一个复选标记....这很容易。
为此我做了以下事情:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableViewMieux deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if (selectedCell.accessoryType == UITableViewCellAccessoryNone)
{
UIImageView *checkBox = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checkbox-pressed.png"]] autorelease];
checkBox.frame = CGRectMake(0, 0, 20, 20);
selectedCell.accessoryView = checkBox;
selectedCell.accessoryType=UITableViewCellAccessoryCheckmark;
}
else if (selectedCell.accessoryType==UITableViewCellAccessoryCheckmark)
{
UIImageView *checkBox = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checkbox.png"]] autorelease];
checkBox.frame = CGRectMake(0, 0, 20, 20);
selectedCell.accessoryView = checkBox;
selectedCell.accessoryType=UITableViewCellAccessoryNone;
}
[tableViewMieux reloadData];
}
一切都很有效,除了当我选择第一个单元格时,它会被勾选并且第五个单元格也会自动被勾选。 依此类推......如果我选择第二个单元格,则会对其进行选中标记...而来自tableView底部的另一个单元格也会被标记。
因此,每次我选择并标记一个单元格时,额外的单元格都会被标记。
Question:Why?What is the right way to do it?
编辑:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableViewMieux dequeueReusableCellWithIdentifier:@"CellRecherchePartenaires"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"CellRecherchePartenaires"] autorelease];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.backgroundView.opaque = NO;
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.opaque = NO;
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.highlightedTextColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.opaque = NO;
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.detailTextLabel.highlightedTextColor = [UIColor whiteColor];
cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
cell.accessoryView=UITableViewCellAccessoryNone;
}
// Set up the cell...
[[cell textLabel] setText: [tableArray objectAtIndex:indexPath.row]] ;
return cell;
}
答案 0 :(得分:2)
添加以下行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if([[tableView cellForRowAtIndexPath:indexPath] accessoryType] == UITableViewCellAccessoryCheckmark)
{
temp = [myarray objectAtInadex:indexpath.row];// change the line according to ur code
//temp is any object.. //array is nsmutable array(global)
[array removeObject:temp];
[tableView cellForRowAtIndexPath:indexPath].accessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]] autorelease];
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone];
return;
}
else
{
//temp is any objecct....//arry is nsmutable array(global)
[array addObject:temp];
[tableView cellForRowAtIndexPath:indexPath].accessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"4.png"]] autorelease];
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
}
}
}
在行索引路径的单元格中
temp = [myarray objectAtInadex:indexpath.row];// change the line according to ur code
if ([array containsObject:temp]) {
cell.accessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"4.png"]] autorelease];
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
}
else
{
cell.accessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]] autorelease];
[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone];
}
在行数
中的行数[array removeAllObjects];
尝试,它会起作用。确定
答案 1 :(得分:1)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", indexPath.section, indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.backgroundView.opaque = NO;
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.opaque = NO;
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.highlightedTextColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.opaque = NO;
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.detailTextLabel.highlightedTextColor = [UIColor whiteColor];
cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
cell.accessoryView=UITableViewCellAccessoryNone;
}
// Set up the cell...
[[cell textLabel] setText: [tableArray objectAtIndex:indexPath.row]] ;
return cell;
}
只需尝试一下......
答案 2 :(得分:0)
我也有这个问题。这很可能是因为你正在重复使用单元格(带有单元标识符。这就是你应该做的)。要解决这个问题,请在代理
中 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
执行以下操作:
static NSString * cellIdentifier = @"Cell";
// Get reusable cell if possible
UITableViewCell * cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:cellIdentifier] autorelease];
}
// Remove checkmark if cell is not the one that should be checked
int idx = [workingDatasource indexOfObject:currentSupposedToBeChecked];
if (idx != indexPath.row)
cell.accessoryType = UITableViewCellAccessoryNone;
else
cell.accessoryType = UITableViewCellAccessoryCheckmark;
编辑:更多解释:
因此, workingDatasource 是包含UITableView数据源的数组, currentSupposedToBeChecked 是包含实际数据的实例变量(必须是其中一个数据)应该检查的数据源)。
因此,在每次触摸时(即在didSelectRowAtIndexPath:
委托中),您应该通过执行
self.currentSupposedToBeChecked = [workingDatasource objectAtIndex:indexPath.row];
答案 3 :(得分:0)
问题为什么:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
这个你在这里调用的方法:UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
返回一个Cell,但苹果设计的方式是每次都不是从头开始创建单元格,而是重复使用单元格,所以你的第5和第20个单元格tableview可能是“同一个”,因为当你向下滚动时,单元格会在视线不在时被释放,滚动到的单元格会在滚动时被创建,这样就会减少用于桌面视图的内存。
关于如何解决这个问题,我不确定,但是你必须......显然看看我在打字时发布的mrd3650解决方案:)
答案 4 :(得分:0)
为了清楚起见,我添加了另一个答案......
我已经编辑了//--Added--->
评论。
如果有什么不明确的地方留下评论。
请注意self.currentlySelected
必须声明为NSString
属性(可以是私有的)
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableViewMieux deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if (selectedCell.accessoryType == UITableViewCellAccessoryNone)
{
UIImageView *checkBox = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checkbox-pressed.png"]] autorelease];
checkBox.frame = CGRectMake(0, 0, 20, 20);
selectedCell.accessoryView = checkBox;
selectedCell.accessoryType=UITableViewCellAccessoryCheckmark;
}
else if (selectedCell.accessoryType==UITableViewCellAccessoryCheckmark)
{
UIImageView *checkBox = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checkbox.png"]] autorelease];
checkBox.frame = CGRectMake(0, 0, 20, 20);
selectedCell.accessoryView = checkBox;
selectedCell.accessoryType=UITableViewCellAccessoryNone;
}
//-----ADDED---->>
int idx = [tableArray indexOfObject:self.currentlySelected];
if (idx != indexPath.row)
cell.accessoryType = UITableViewCellAccessoryNone;
else
cell.accessoryType = UITableViewCellAccessoryCheckmark;
//-----ADDED----<<
[tableViewMieux reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableViewMieux dequeueReusableCellWithIdentifier:@"CellRecherchePartenaires"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"CellRecherchePartenaires"] autorelease];
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.backgroundView.opaque = NO;
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.opaque = NO;
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.highlightedTextColor = [UIColor whiteColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.opaque = NO;
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.detailTextLabel.highlightedTextColor = [UIColor whiteColor];
cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
cell.accessoryView=UITableViewCellAccessoryNone;
}
// Set up the cell...
[[cell textLabel] setText: [tableArray objectAtIndex:indexPath.row]] ;
//----ADDED--->>
// Save currently selected text - NOTE this must be a property
self.currentlySelected = [tableArray objectAtIndex:indexPath.row];
//----ADDED---<<
return cell;
}