gpt4 book ai didi

ios - 如何在 iphone 中的选定单元格上显示选择箭头图像

转载 作者:行者123 更新时间:2023-11-28 22:23:53 26 4
gpt4 key购买 nike

我有 tableView with custom cell,它工作正常,但是当我选择按钮时,它会显示选定的图像,而不是简单的框图像。

问题:当我选择任何单元格时,它还会选择任何其他单元格并显示所选的单元格按钮图像。这是我的代码

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
if (tableView == employeeTableView)
{
employeeCell = (EmployeeCell *)[employeeTableViewdequeueReusableCellWithIdentifier:@"cell"];
if (!employeeCell)
{
employeeCell = [[EmployeeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"EmployeeCell" owner:self options:nil];

employeeCell = (EmployeeCell *)[nib objectAtIndex:0];
}

employeeTableView.backgroundColor=[UIColor clearColor];
employeeTableView.backgroundView=nil;
employeeCell.emplyeeName.font=[UIFont fontWithName:@"Arial" size:13];
employeeCell.employeeDepartement.font=[UIFont fontWithName:@"Arial" size:13];
employeeCell.EmployeePosition.font=[UIFont fontWithName:@"Arial" size:13];
Coffee *coffeeObj=[appDelegate.employeeArray objectAtIndex:indexPath.row];
employeeCell.employeeDepartement.textColor = [UIColor brownColor];
employeeCell.EmployeePosition.textColor = [UIColor brownColor];

employeeCell.emplyeeName.textColor = [UIColor brownColor];

employeeCell.emplyeeName.text = coffeeObj.employeeName;
employeeCell.EmployeePosition.text = coffeeObj.employeeJob;
employeeCell.employeeDepartement.text = coffeeObj.departmentName;

if ([employeeCell.selectionButton isSelected])
{
employeeCell.selectedImage.image =[UIImage imageNamed:@"CheckArrow"];

}
else
{
employeeCell.selectedImage.image = [UIImage imageNamed:@"boxbutton"];
}
[employeeCell.selectionButton addTarget:self action:@selector(setSelectCell:) forControlEvents:UIControlEventTouchUpInside];

[employeeCell.selectionButton setTag:indexPath.row];

return employeeCell;

}

if (tableView == trainerTableView)
{

UITableViewCell *cell = [trainerTableView dequeueReusableCellWithIdentifier:@"Cell"];
if (!cell) {
cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];

}
cell.textLabel.text = @"Calvin Banks";
// [appDelegate.trainerArray objectAtIndex:indexPath.row];

return cell;
}
return 0;

}


-(void)setSelectCell:(id)Sender
{
CGPoint point =[Sender convertPoint:CGPointZero toView:employeeTableView];
NSIndexPath *indexPath =[employeeTableView indexPathForRowAtPoint:point];
NSLog(@"indexPath %@",indexPath);
Coffee *cofee;

cofee = [appDelegate.employeeArray objectAtIndex:[Sender tag]];

employeeCell =(EmployeeCell *)[employeeTableView cellForRowAtIndexPath:indexPath];

if ([employeeCell.selectionButton isSelected]) {
[employeeCell.selectionButton setSelected:NO];

employeeCell.selectedImage.image =[UIImage imageNamed:@"boxbutton"];

}
else
{
[employeeCell.selectionButton setSelected:YES];
employeeCell.selectedImage.image =[UIImage imageNamed:@"CheckArrow"];

[selectedEmployees addObject:cofee];
}

}

最佳答案

试试这个:

//把它放在.h文件中 mutable arSelectedRows;

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *CellIdentifier = @"Cell56756";
if (tableView == employeeTableView)
{
EmployeeCell *Cell = (EmployeeCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (Cell == nil)
{
NSArray *topLevelObject = [[NSBundle mainBundle] loadNibNamed:@"EmployeeCell" owner:self options:nil];
for (id currentobject in topLevelObject)
{
if ([currentobject isKindOfClass:[UITableViewCell class]])
{
Cell = (EmployeeCell *) currentobject;
break;
}
}
}

employeeTableView.backgroundColor=[UIColor clearColor];
employeeTableView.backgroundView=nil;


Cell.emplyeeName.font=[UIFont fontWithName:@"Arial" size:13];
Cell.employeeDepartement.font=[UIFont fontWithName:@"Arial" size:13];
Cell.EmployeePosition.font=[UIFont fontWithName:@"Arial" size:13];

Cell.employeeDepartement.textColor = [UIColor brownColor];
Cell.EmployeePosition.textColor = [UIColor brownColor];
Cell.emplyeeName.textColor = [UIColor brownColor];

Coffee *coffeeObj=[appDelegate.employeeArray objectAtIndex:indexPath.row];
Cell.emplyeeName.text = coffeeObj.employeeName;
Cell.EmployeePosition.text = coffeeObj.employeeJob;
Cell.employeeDepartement.text = coffeeObj.departmentName;

if([arSelectedRows containsObject:indexPath]) {
Cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
Cell.accessoryType = UITableViewCellAccessoryNone;
}
return Cell;

}
if (tableView == trainerTableView)
{
UITableViewCell *cell = [trainerTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = @"Calvin Banks";
return cell;
}
return 0;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == employeeTableView)
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if(cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[arSelectedRows addObject:indexPath];
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
[arSelectedRows removeObject:indexPath];
}
NSLog(@"id are here :%@",arSelectedIDs);
}

[tableView deselectRowAtIndexPath:indexPath animated:YES];

}

它会有所帮助。

快乐的编码......

关于ios - 如何在 iphone 中的选定单元格上显示选择箭头图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19538025/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com