长时间在互联网上搜索,但没有得到合适的答案。我将UITableView置于编辑模式并一次选择多行。它工作得很好,但我想将复选标记的颜色从红色变为蓝色,就像在iPhone电子邮件应用程序中一样。
任何帮助都将不胜感激。
编辑版本:
这是我的代码......
在我的ViewDidLoad函数中:
- (void)viewDidLoad
{
...
[deviceTableVIew setAllowsSelectionDuringEditing:YES];
[deviceTableVIew setAllowsMultipleSelectionDuringEditing:YES];
[super viewDidLoad];
}
我有两个UIButtons,它们为tableview设置编辑模式如下:
-(IBAction)control:(id)sender{
btnControl.enabled = false;
btnControl.hidden = true;
btnCancel.enabled = true;
btnCancel.hidden = false;
stateToggleToolbar.hidden = false;
[self.deviceTableVIew setEditing:YES animated:YES];
}
-(IBAction)cancel:(id)sender{
btnCancel.enabled = false;
btnCancel.hidden = true;
btnControl.enabled = true;
btnControl.hidden = false;
stateToggleToolbar.hidden = true;
[self.deviceTableVIew setEditing:NO animated:YES];
}
UITableView委托方法是:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//setting up the cells here
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
...
if ([tableView isEditing] == YES) {
// Do Nothing
}else{
[self.navigationController pushViewController:viewController animated:YES];
}
}
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return 3;
}
答案 0 :(得分:10)
一行解决方案就在这里)
只需在 cellForRow:atIndexPath 方法中设置单元格 tintColor 。
cell.tintColor = UIColor.red
下面是快速代码:
func tableView(tableView: UITableView,
cellForRowAtIndexPath indexPath: NSIndexPath)
-> UITableViewCell
{
//get cell to configure from tableView
let cell = tableView.dequeueReusableCellWithIdentifier("myCellIdentifier", forIndexPath: indexPath) as UITableViewCell
//This line will change checkmark color of your awesome cell
cell.tintColor = UIColor.redColor()
// Configure cell
// ...
//work is done! Let put the cell back to the tableView))
return cell
}
答案 1 :(得分:0)
您不能更改复选标记颜色,因为它不受支持。您可以做的是制作图像并在您的cellForRowAtIndexPath中添加以下代码
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
selected=[[NSMutableArray alloc] init];
for(int i=0;i<10;i++){
[selected addObject:@"0"];
}
}
- (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];
}
cell.textLabel.text=[NSString stringWithFormat:@"hello %d",indexPath.row];
if([[selected objectAtIndex:indexPath.row] isEqualToString:@"1"]){
cell.accessoryType=UITableViewCellAccessoryCheckmark;
}
else{
cell.accessoryType=UITableViewCellAccessoryNone;
}
// Configure the cell...
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if([[selected objectAtIndex:indexPath.row] isEqualToString:@"0"]){
[selected replaceObjectAtIndex:indexPath.row withObject:@"1"];
}
else{
[selected replaceObjectAtIndex:indexPath.row withObject:@"0"];
}
[tbl reloadData];
}
答案 2 :(得分:-1)
您可以更改复选标记图像。使用复选标记图像和颜色创建复选标记图像。 然后在下面的代码中替换图像名称。这适用于iOS 6及以下版本
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
if (self.isEditing) {
if (selected){
for (UIView *subview in self.subviews) {
if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellEditControl"]) {
for (UIView *aView in subview.subviews) {
if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellEditControl"]) {
[aView.layer setContents:(id)[UIImage imageNamed:@"delete_check.png"].CGImage];
}
}
}
}
}
}
}