• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - UItableView 用 UIButton 替换复选标记

[复制链接]
菜鸟教程小白 发表于 2022-12-13 09:43:57 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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

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

- (void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)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 *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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 actionselector(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)doneBtnUIButton*)sender
{
UIButton *btn = (UIButton*)sender;
}

提前致谢。



Best Answer-推荐答案


全局声明这些变量

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

在'viewDidLoad'中

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

在'cellForRowAtIndexPath'中

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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)tableViewUITableView *)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)selectionButtonActionUIButton*)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”包含选定行的索引路径

关于ios - UItableView 用 UIButton 替换复选标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31875235/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap