开发者

How to place the checkbox in tableview cells? [closed]

开发者 https://www.devze.com 2023-03-04 20:01 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this questio开发者_JAVA百科n so that it can be reopened, visit the help center. Closed 11 years ago.

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


you can do something like this

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

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.

How to place the checkbox in tableview cells? [closed]

How to place the checkbox in tableview cells? [closed]

Hope it helps..Cheers

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


Use UIButton and set it's default and selected image as unchecked and then checked.
Here is the tutorial, and there are many similar questions related to this on stack,before putting same question again, try to search it.

0

精彩评论

暂无评论...
验证码 换一张
取 消