如何将复选框放置在tableview单元格中? [英] How to place the checkbox in tableview cells?

查看:77
本文介绍了如何将复选框放置在tableview单元格中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hai,我想将复选框放置在表格视图的每一行中.请提供任何示例代码,以将复选框放置在表格视图单元格中

hai,I want to place the checkbox in every row of tableview.Please provide any example code for place the checkbox in tableview cells

推荐答案

您可以执行以下操作

- (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];

        UIButton * btnShare=[[UIButton alloc] initWithFrame:CGRectMake(270,0,32,32)];
        [btnShare setImage:[UIImage imageNamed:@"check-box.png"] forState:UIControlStateNormal];
        [btnShare setImage:[UIImage imageNamed:@"check-box-selected.png"] forState:UIControlStateSelected];
        [btnShare addTarget:self  action:@selector(accessoryButtonTapped:withEvent:) forControlEvents: UIControlEventTouchUpInside];

        cell.accessoryView = btnShare;
        [btnShare release];         
    }    

    BOOL isShared = ([arrayOfSelectedIndex indexOfObject:[NSNumber numberWithInt:indexPath.row]] != NSNotFound);
    UIButton* btnShare1 = (UIButton*)cell.accessoryView;
    btnShare1.selected = isShared;
    [btnShare1 setNeedsDisplay];
    return cell;
}

其中arrayOfSelectedIndex是一个包含所有选定索引的数组.

where arrayOfSelectedIndex is an array containing all the indexes which are selected.

几乎没有其他方法可以帮助对行进行多重选择,但是我确信上面的代码段可以使您对如何获取单元格中的复选框有所了解.

There are few more methods aiding the multi selection of rows but I am sure the above snippet can give you some idea as to how to get check boxes in cells.

希望有帮助..欢呼

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{   
    [tableView reloadData];
}

- (void) accessoryButtonTapped: (UIControl *) button withEvent: (UIEvent *) event{
    NSIndexPath * indexPath = [neighbourTableView indexPathForRowAtPoint: [[[event touchesForView: button] anyObject] locationInView: neighbourTableView]];
    if ( indexPath == nil )
        return;
    [arrayOfSelectedIndex removeAllObjects];
    [arrayOfSelectedIndex addObject:[NSNumber numberWithInt:indexPath.row]];
    [self tableView:neighbourTableView accessoryButtonTappedForRowWithIndexPath:indexPath];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [arrayOfSelectedIndex removeAllObjects];
    [arrayOfSelectedIndex addObject:[NSNumber numberWithInt:indexPath.row]];
    [tableView reloadData];
}

这篇关于如何将复选框放置在tableview单元格中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆