IOS adds the selected answer on the button in the TableView cell

Source: Internet
Author: User

We all know that the tableview of the re-use of course do not know the words can be a message or on the Internet to find here I do not say more;

Red is the selected state, but at this point we will find that the button in the new cell is selected when the cell disappears. Words not much to say below the code of the workaround!

-(Nsmutablearray *) boolarr{

Create an array where the subscript of the NSNumber object in the array is corresponding to Indexpath one by one here, I'll give him 100. Assigning values according to their own circumstances

if (_boolarr==nil) {

Nsmutablearray *arr = [Nsmutablearray array];

for (int i=0; i<100; i++) {

NSNumber *anumber = [NSNumber numberwithinteger:100];//here to Anumber assignment 100 is the array of 100, a value of 100 NSNumber object, Notice here that I have changed the anumber to indicate whether the button is selected.

[Arr Addobject:anumber];

}

_boolarr = arr;

}

return _boolarr;

}

//////////

-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (Nsinteger) section{

Number of rows per sector

return 100;

}

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{

TableViewCell1 *cell = [TableView dequeuereusablecellwithidentifier:identifier forindexpath:indexpath];//Initialize cell

__block Viewcontroller *vc = self;

UIButton *but = (UIButton *) [cell viewwithtag:100];//Gets the button of the cell

Use block to transmit value

Cell.block = ^ () {

Remove the cell's indexpath.row corresponding to the Bollarr value

NSNumber *bo= Vc.boolarr[indexpath.row];

Change the value of Bo with

Bo = [NSNumber numberwithinteger:1];

Then replace the original value of Bollarr

[Vc.boolarr ReplaceObjectAtIndex:indexPath.row Withobject:bo];

Change but the selected state to Yes

but.selected = YES;

};

Use this method to determine if but is selected

[Self addselected:but addIndexPath:indexPath.row];

return cell;

}

-(void) addselected: (UIButton *) sender Addindexpath: (Nsinteger) indexpath{

The value in the Self.bollarr corresponding to the row of the cell is taken out

NSNumber *bo= Self.boolarr[indexpath];

Then convert to Nsinteger type

Nsinteger bolltge = Bo.integervalue;

Then determine if the value has been changed

100 has not been changed no

1 has been modified Yes

if (bolltge==100) {

sender.selected = NO;

}else{

sender.selected = YES;

}

}

Because of the time, I did not write a method to uncheck the state, just write a boll value to judge it. I hope I can help everyone. I'm also a beginner of iOS

If there's any easier way to do it here.

I qq:627400364 really look for and I like to walk in programming this way of friends let us help each other AH ~ ~ ~ ~ ~ ~ ~ Pain and happy haha

IOS adds the selected answer on the button in the TableView cell

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.