我想将checkmark作为附件类型应用于合适视图中的选定项目。但是对所有重复使用的单元格都会显示复选标记。我们怎样才能避免这个问题? 代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if ([[[data objectAtIndex:indexPath.row] ringtone_id] isEqualToString:selId] ) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
// Configure the cell...
cell.textLabel.text = [[[data objectAtIndex:indexPath.row] ringtone_name] stringByDeletingPathExtension];
return cell;
}
答案 0 :(得分:1)
试试这个:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
// Configure the cell...
if ([[[data objectAtIndex:indexPath.row] ringtone_id] isEqualToString:selId] ) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.text = [[[data objectAtIndex:indexPath.row] ringtone_name] stringByDeletingPathExtension];
return cell; }
希望这会对你有所帮助。
答案 1 :(得分:0)
您必须添加else
案例以取消标记您不想标记的单元格,如下所示:
if ([[[data objectAtIndex:indexPath.row] ringtone_id] isEqualToString:selId] ) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
答案 2 :(得分:0)
以这种方式尝试:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
// Configure the cell...
if ([[[data objectAtIndex:indexPath.row] ringtone_id] isEqualToString:selId] ) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.text = [[[data objectAtIndex:indexPath.row] ringtone_name] stringByDeletingPathExtension];
return cell;
}