Need to create a select all button for UITableView and add selections to an array in iOS

后端 未结 3 544
时光取名叫无心
时光取名叫无心 2021-01-23 16:49

I have a UITableViewController that shows a list of items from NSMutableArray, along with a button that serves as a check box that the user can select/deselect. I am able to su

相关标签:
3条回答
  • 2021-01-23 17:36

    Take another NSMUtableArray as SelectedArray

    in didselectRowAtIndexPath row You can Add remove objects from SelectedArray.

    You can select a cell calling table view's selectRowAtIndexPath method:

    [yourtable selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
    

    Put for loop for selectedArray to putcheckbutton only in selected cells.

    in Your CellForRow Method Use this

    testButton.tag=indexpath.row;
    

    you can select All row and All Section by below method.

    for (int i = 0; i < [ptableView numberOfSections]; i++) {
        for (int j = 0; j < [ptableView numberOfRowsInSection:i]; j++) {
            NSUInteger ints[2] = {i,j};
            NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:ints length:2];
                UITableViewCell *cell = [ptableView cellForRowAtIndexPath:indexPath];
               //Here is your code
    
          UiButton *btn = (UiButton*)[cell.contentView viewWithTag:j]
    
    if( [[btn imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:@"CheckBox1.png"]])
        {
            [btn setImage:[UIImage imageNamed:@"CheckBox2.png"] forState:UIControlStateNormal];
            // other statements
        }
        else
        {
            [btn setImage:[UIImage imageNamed:@"CheckBox1.png"] forState:UIControlStateNormal];
            // other statements
        }
            }
        }
    
    0 讨论(0)
  • 2021-01-23 17:47

    Here i m Posting Another Answer so that it might help to others who wants to make this from Scratch and remove any Confusions.

    Instead of Adding Button to tableViewCell ,you can use ImageView as AccessoryView.

    UIImageView *imageView1 = [[[UIImageView alloc] init] autorelease];
    imageView1.tag = indexpath.row;
    
    [cell.accessoryView addSubview:imageView1];
    

    And later get subview using -viewWithTag: method:

    UIImageView *getImageView1 = (UIImageView*)[cell.accessoryView viewWithTag:rowNumber];
    getImageView1.image=[UIImage ImageNamed:@"checked.png"];
    

    By the Above code we can change the image of imageView in selectRowAtindexPath

    From my first Answer you can use the code to Select all or Multiple cell of tableView.

    0 讨论(0)
  • 2021-01-23 17:48

    If you want that in UITableView header than use following method:

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    

    Return UIView Object from this method which contain checkbox and what else you want.

    TO check uncheck maintain an array where you can store that a checkbox is checked or not.

    When user check/uncheck header change array and reload table.

    0 讨论(0)
提交回复
热议问题