【问题标题】:UItableView Replacing Checkmarks with UIButtonUItableView 用 UIButton 替换复选标记
【发布时间】:2015-08-21 16:43:19
【问题描述】:

我只想使用UIButton 代替复选标记我成功添加按钮并使其在选择行时作为复选标记但只有行选择是工作按钮不明智不是我想让它工作按钮以及单元格选择截图:

http://i.imgur.com/aXiBsfU.png

http://i.imgur.com/hQddEGk.png

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row==0)
{
    if (selectAll==0)
    {
        selectAll=1;
        [self.arrForIndPaths removeAllObjects];
    }
    else
    {
        selectAll=0;
        [self.arrForIndPaths removeAllObjects];
    }
}
else
{
    if (selectAll!=1)
    {
        if([self.arrForIndPaths containsObject:indexPath])
        {
            [self.arrForIndPaths removeObject:indexPath];
        }
        else
        {
            [self.arrForIndPaths addObject:indexPath];
        }
    }
    else
    {
        selectAll=0;
        if([self.arrForIndPaths containsObject:indexPath])
        {
            [self.arrForIndPaths removeObject:indexPath];
        }
        else
        {[self.arrForIndPaths addObject:indexPath];}}}[tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static const NSInteger btnTag = 1;
static NSString *MyIdentifier = @"SearchResult";
UITableViewCell *cell;
if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
UILabel *lblText = [[UILabel alloc]initWithFrame:CGRectMake(40, 0, 400, 55)];
UIButton *btnCheck = [[UIButton alloc]initWithFrame:CGRectMake(845, 10, 35, 28)];
btnCheck.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
btnCheck.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[btnCheck setImage:[UIImage imageNamed:@"checkbox_white.png"] forState:UIControlStateNormal];
[btnCheck setImage:[UIImage imageNamed:@"checked_white.png"] forState:UIControlStateSelected];
[btnCheck setImage:[UIImage imageNamed:@"checked_white.png"] forState:UIControlStateHighlighted];
[btnCheck addTarget:self action:@selector(doneBtn:) forControlEvents:UIControlEventTouchUpInside];
btnCheck.tag = btnTag;
[cell.contentView addSubview:lblText];
[cell addSubview:btnCheck];
if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
{
    if (indexPath.row==0)
    {
        lblText.textColor =[self colorWithHexString:@"333333"];
        lblText.font=[UIFont fontWithName:@"VERDANA" size:30];
    }
    else
    {
        lblText.textColor=[self colorWithHexString:@"333333"];
        lblText.font=[UIFont fontWithName:@"VERDANA" size:20];
    }
}
else
{
    if (indexPath.row==0)
    {
        lblText.textColor=[self colorWithHexString:@"333333"];
        lblText.font=[UIFont fontWithName:@"VERDANA" size:18];
    }
    else
    {
        lblText.textColor=[self colorWithHexString:@"333333"];
        lblText.font=[UIFont fontWithName:@"VERDANA" size:16];
    }
}
lblText.text=[arrFacilities objectAtIndex:indexPath.row];
lblText.textAlignment=NSTextAlignmentLeft;
if (selectAll==1)
{
    [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    [btnCheck setImage:[UIImage imageNamed:@"checked_white.png"] forState:UIControlStateNormal];
}
else
{
    if([self.arrForIndPaths containsObject:indexPath])
    {
        [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
        [btnCheck setImage:[UIImage imageNamed:@"checked_white.png"] forState:UIControlStateNormal];
    }
    else
    {
        [cell setAccessoryType:UITableViewCellAccessoryNone];
        [btnCheck setImage:[UIImage imageNamed:@"checkbox_white.png"]forState:UIControlStateNormal];
    }
}
cell.textLabel.numberOfLines=0;
cell.textLabel.lineBreakMode=NSLineBreakByWordWrapping;
return cell;
}
- (void)doneBtn:(UIButton*)sender
{
UIButton *btn = (UIButton*)sender;
}

提前致谢。

【问题讨论】:

  • 你的意思是在单元格选择上它可以工作,但是在按钮上点击它不起作用,你希望它对按钮点击和单元格选择都有效。
  • 仍然单击单元格工作,但按钮工作(选择所有按钮不起作用)实际上我希望按钮工作与工作单元格相同,或者单击单元格或按要求选择按钮,谢谢
  • @iDeepak 我认为您的全选是 tableViewCell?对吗?
  • @iDeepak 你需要'全选'单元格的最后一个刻度(没有正方形的刻度)吗??
  • @Shebin Koshy 我需要作为单元格选择,如果按钮将选择单元格将选择并且单元格将选择按钮也选择反之亦然.. 按钮不选择图像是带有黑色边框的白色方块并被选中我创建的这些图像在屏幕截图中显示为蓝色框

标签: ios uitableview uibutton


【解决方案1】:

全局声明这些变量

BOOL isSelectedAll;
NSMutableArray *arraySelectedRows;
int allDataCount; // this may be your arrayWithData.count

在'viewDidLoad'中

isSelectedAll = NO;
allDataCount = 10;
arraySelectedRows = [[NSMutableArray alloc]init];

在'cellForRowAtIndexPath'中

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ce" forIndexPath:indexPath];
CustomTableViewCell *cell = [[CustomTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cee"];
cell.textLabel.text = @"Test";
UIButton *buttonSelection = [[UIButton alloc]initWithFrame:CGRectMake(tableView.frame.size.width - 100, 8, 50, 50)];
buttonSelection.tag = indexPath.row;
if(isSelectedAll)
{
    [buttonSelection setImage:[UIImage imageNamed:@"tick"] forState:UIControlStateNormal];//Rename imageName as yours
}
else if([arraySelectedRows containsObject:indexPath]&&indexPath.row!=0)
{
    [buttonSelection setImage:[UIImage imageNamed:@"tick"] forState:UIControlStateNormal];//Rename imageName as yours
}
else
{
    [buttonSelection setImage:[UIImage imageNamed:@"untick"] forState:UIControlStateNormal];//Rename imageName as yours
}

[buttonSelection addTarget:self action:@selector(selectionButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:buttonSelection];
return cell;
}

在“didSelectRowAtIndexPath”中

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:     (NSIndexPath *)indexPath
{
if (indexPath.row == 0)
{
    //select all button
    if (isSelectedAll)
    {
        isSelectedAll = NO;
        [arraySelectedRows removeAllObjects];
    }
    else
    {
        [arraySelectedRows removeAllObjects];
        for (int i = 1; i<allDataCount; i++)
        {
            [arraySelectedRows addObject:[NSIndexPath indexPathForRow:i inSection:0]];//section number may be defer for you
        }
        isSelectedAll = YES;
    }
}
else
{
    if([arraySelectedRows containsObject:indexPath])
    {
        [arraySelectedRows removeObject:indexPath];

    }
    else
    {
        [arraySelectedRows addObject:indexPath];
    }
    if ((allDataCount - 1) == arraySelectedRows.count)
    {
        isSelectedAll = YES;
    }
    else
    {
        isSelectedAll = NO;
    }
}
[self.tableView reloadData];
}

如果你需要在按钮操作中做同样的事情

-(void)selectionButtonAction:(UIButton*)button
{
if (button.tag == 0)
{
    //select all button
    if (isSelectedAll)
    {
        isSelectedAll = NO;
        [arraySelectedRows removeAllObjects];
    }
    else
    {
        [arraySelectedRows removeAllObjects];
        for (int i = 1; i<allDataCount; i++)
        {
            [arraySelectedRows addObject:[NSIndexPath indexPathForRow:i inSection:0]];//section number may be defer for you
        }
        isSelectedAll = YES;
    }
}
else
{
    NSIndexPath *buttonIndexPath = [NSIndexPath indexPathForRow:button.tag inSection:0];//section number may be defer for you
    if([arraySelectedRows containsObject:buttonIndexPath])
    {
        [arraySelectedRows removeObject:buttonIndexPath];

    }
    else
    {
        [arraySelectedRows addObject:buttonIndexPath];
    }
    if ((allDataCount - 1) == arraySelectedRows.count)
    {
        isSelectedAll = YES;
    }
    else
    {
        isSelectedAll = NO;
    }
}
[self.tableView reloadData];
}

“arraySelectedRows”包含选定行的索引路径

【讨论】:

  • 哇真的太棒了,它就像奇迹一样修复了一切,非常感谢非常感谢非常
  • 我也有一些关于视图折叠和展开的问题你能帮我吗????
  • 已经在这里问过请看stackoverflow.com/questions/32176153/…
  • 我以不同的方式解决了这个问题,但我需要您的帮助以进行实施,您能告诉我如何联系您吗??
  • @iDeepak 现在有什么问题?你有这个或那个xib问题的问题吗?
【解决方案2】:

请试试这个方法

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MessageViewCell *cell =(MessageViewCell *)[tableView dequeueReusableCellWithIdentifier:@"cell"];
    if(cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MessageViewCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    cell.lblUsername.text  = [arrUsername objectAtIndex:indexPath.row];

    NSURL *URL =[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",_URL_IMAGEURL,[arrImages objectAtIndex:indexPath.row]]];

    cell.imgView.tag = indexPath.row;

    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openProfile:)];

    tap1.numberOfTapsRequired = 1;

    cell.imgView.userInteractionEnabled = YES;

    [cell.imgView addGestureRecognizer:tap1];

    cell.imgView.image = [UIImage imageNamed:@"NoImage.png"];

    [[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget:cell.imgView];

    cell.imgView.imageURL = URL;

    cell.imgView.layer.cornerRadius = cell.imgView.frame.size.width/2;

    [cell.imgView setClipsToBounds:YES];

    cell.btnCheckBox.tag = indexPath.row;

    [cell.btnCheckBox setHidden:false];

    if([[[arrFriends objectAtIndex:indexPath.row] valueForKey:@"markedToDelete"] boolValue]){
        [cell.btnCheckBox setSelected:TRUE];
    }else{
        [cell.btnCheckBox setSelected:FALSE];
    }


    [cell.btnCheckBox addTarget:self **action:@selector(checkBoxClick:)** forControlEvents:UIControlEventTouchUpInside];

    cell.lblMsg.text = [arrLastMessage objectAtIndex:indexPath.row];

    return cell;
}
/////
-(void)checkBoxClick:(UIButton *)sender
{
    UIButton *btn = (UIButton *)sender;
    NSIndexPath *indexPath = [tblMessages indexPathForCell:(UITableViewCell *)btn.superview.superview];
    [sender setSelected:!sender.isSelected];
    if (sender.isSelected)
    {
        arrFriends[sender.tag][@"markedToDelete"] = @YES;
        if(![arrDeleteUsername containsObject:[arrOppUsername objectAtIndex:indexPath.row]])
        {
            [arrDeleteUsername addObject:[arrOppUsername objectAtIndex:indexPath.row]];
        }
    }
    else
    {
        [arrDeleteUsername removeObject:[arrOppUsername objectAtIndex:indexPath.row]];
        arrFriends[sender.tag][@"markedToDelete"] = @NO;
    }
}

【讨论】:

    【解决方案3】:

    您的按钮单击操作代码看起来没有做任何事情。只需将以下代码添加到您的项目中,它应该可以工作:

    - (void)doneBtn:(UIButton*)sender{
    if ([sender tag]==0)
    {
        if (selectAll==0)
        {
            selectAll=1;
            [self.arrForIndPaths removeAllObjects];
        }
        else
        {
            selectAll=0;
            [self.arrForIndPaths removeAllObjects];
        }
    }
    else
    {
        if (selectAll!=1)
        {
            if([self.arrForIndPaths containsObject:[NSIndexPath indexPathForRow:[sender tag] inSection:0]])
            {
                [self.arrForIndPaths removeObject:[NSIndexPath indexPathForRow:[sender tag] inSection:0]];
            }
            else
            {
                [self.arrForIndPaths addObject:[NSIndexPath indexPathForRow:[sender tag] inSection:0]];
            }
        }
        else
        {
            selectAll=0;
            if([self.arrForIndPaths containsObject:[NSIndexPath indexPathForRow:[sender tag] inSection:0]])
            {
                [self.arrForIndPaths removeObject:[NSIndexPath indexPathForRow:[sender tag] inSection:0]];
            }
            else
            {
             [self.arrForIndPaths addObject:[NSIndexPath indexPathForRow:[sender tag] inSection:0]];
            }
           }
        }
    [tableView reloadData];
    }
    

    或进行相应的更改。

    【讨论】:

    • 确保您正确设置按钮标签而不是您添加的标签。
    • 我正在使用 button.tag = indexpath.row ,任何其他建议非常感谢您的回复
    • 它修复了 add set button.tag = 0 ,仅选择按钮使所有按钮都被选中,但它需要一次一个(全选将同时全选),任何其他建议,谢谢很多跨度>
    • 感谢您的所有努力,我真的很感激..没有尝试过这个,仍然选择所有单击按钮,否则单元格选择工作正常,不能像单元格选择一样完成吗? ?
    • 你好,仍在等待回复我该如何解决这个问题
    猜你喜欢
    • 2015-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-24
    • 1970-01-01
    • 1970-01-01
    • 2013-06-29
    • 2015-03-11
    相关资源
    最近更新 更多