Here is my problem, i have a custom cell in that 1 label and 4 buttons like question and answers,when the user click on the button 1 remaining 3 button go like this,
When the user scroll the cell it is not saving the what the user selected button i mean to say user click on the what ever the button it is not saving.
Here is my code,
ViewDidLoad()
{
testArray = [[NSMutableArray alloc]init];
for (int i =0; i<[mainArray count]; i++)
{
[testArray addObject:#"Unchecked"];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = #"Cell";
ContestQATableViewCell *cell =(ContestQATableViewCell *)[tableViewQA dequeueReusableCellWithIdentifier:cellId];
if (cell==nil)
{
NSArray *myNib;
myNib =[[NSBundle mainBundle]loadNibNamed:#"ContestQATableViewCell" owner:self options:nil];
cell = (ContestQATableViewCell *)[myNib lastObject];
}
cell.textLabel.text = [mainArray objectAtIndex:indexPath.row];
if([[testArray objectAtIndex:indexPath.row] isEqualToString:#"Unchecked"])
[cell.answer1 setImage:[UIImage imageNamed:#"RadioUnChecked"] forState:UIControlStateNormal];
else
[cell.answer1 setImage:[UIImage imageNamed:#"RadioChecked"] forState:UIControlStateNormal];
cell.answer1.tag = indexPath.row;
[cell.answer1 addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:cell.answer1];
return cell;
[cell.answer1 addTarget:self action:#selector(buttonsClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.answer2 addTarget:self action:#selector(buttonsClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.answer3 addTarget:self action:#selector(buttonsClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.answer4 addTarget:self action:#selector(buttonsClicked:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
and here is my add target method
-(void)buttonsClicked:(id)sender
{
UIButton *btn=(UIButton *)sender;
ContestQATableViewCell * cell=(ContestQATableViewCell *) [btn.superview superview];
if (cell.answer1.tag==btn.tag)
{
[cell.answer1 setImage:[UIImage imageNamed:#"RadioChecked"] forState:UIControlStateNormal];
}else{
[cell.answer1 setImage:[UIImage imageNamed:#"RadioUnChecked"] forState:UIControlStateNormal];
}
if (cell.answer2.tag==btn.tag)
{
[cell.answer2 setImage:[UIImage imageNamed:#"RadioChecked"] forState:UIControlStateNormal];
}else{
[cell.answer2 setImage:[UIImage imageNamed:#"RadioUnChecked"] forState:UIControlStateNormal];
}
if (cell.answer3.tag==btn.tag)
{
[cell.answer3 setImage:[UIImage imageNamed:#"RadioChecked"] forState:UIControlStateNormal];
}else{
[cell.answer3 setImage:[UIImage imageNamed:#"RadioUnChecked"] forState:UIControlStateNormal];
}
if (cell.answer4.tag==btn.tag)
{
[cell.answer4 setImage:[UIImage imageNamed:#"RadioChecked"] forState:UIControlStateNormal];
}else{
[cell.answer4 setImage:[UIImage imageNamed:#"RadioUnChecked"] forState:UIControlStateNormal];
}
}
For Check-Uncheck functionality only buttonClicked: method is not enough. You will have also put the condition in cellForRowAtIndexPath: method for which button is selected or which in unselected because cellForRowAtIndexPath: method will call each time when you will scroll your UITableView and cells will be refresh.
for example
I explain in step by step
Step-1
Create the two arrays one for gloabally another one for checking purpose
#interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
{
NSMutableArray *arrayforCheckUnchek; // handle which button is selected or which is unselected
NSMutableArray *originalArray; // original Array
}
Step-2
allocating the memory for arrays
- (void)viewDidLoad
{
[super viewDidLoad];
arrayforCheckUnchek = [[NSMutableArray alloc]init];
originalArray = [[NSMutableArray alloc]initWithObjects:#"cell1",#"cell2",#"cell3",#"cell4",#"cell5", nil];
// setting all cell initilayy at un check
for(int i=0; i<[originalArray count]; i++)
{
[arrayforCheckUnchek addObject:#"Unchecked"];
}
}
Step-2
setup your datasource Methods
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [originalArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *Identifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [originalArray objectAtIndex:indexPath.row];
UIButton *radiobutton = [UIButton buttonWithType:UIButtonTypeCustom];
[radiobutton setFrame:CGRectMake(270.0, 7.0, 30.0, 30.0)]; // customize the frames
if([[arrayforCheckUnchek objectAtIndex:indexPath.row] isEqualToString:#"Unchecked"])
[radiobutton setImage:[UIImage imageNamed:#"RadioUnChecked"] forState:UIControlStateNormal];
else
[radiobutton setImage:[UIImage imageNamed:#"RadioChecked"] forState:UIControlStateNormal];
radiobutton.tag = indexPath.row;
[radiobutton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:radiobutton];
return cell;
}
Step-3
here selected button index you can change the image
-(void)buttonClicked:(UIButton *)sender
{
//Getting the indexPath of cell of clicked button
CGPoint touchPoint = [sender convertPoint:CGPointZero toView:yourtableviewname];
NSIndexPath *indexPath = [yourtableviewname indexPathForRowAtPoint:touchPoint];
//Checking the condition button is checked or unchecked.
if([[arrayforCheckUnchek objectAtIndex:indexPath.row] isEqualToString:#"Unchecked"])
{
[sender setImage:[UIImage imageNamed:#"RadioChecked"] forState:UIControlStateNormal];
[arrayforCheckUnchek replaceObjectAtIndex:indexPath.row withObject:#"Checked"];
}
else
{
[sender setImage:[UIImage imageNamed:#"RadioUnChecked"] forState:UIControlStateNormal];
[arrayforCheckUnchek replaceObjectAtIndex:indexPath.row withObject:#"Unchecked"];
}
}
Related
in my custom cell lot of question and answers are there, in each section i want to select any one button,that button how can store it? in CellForRowAtIndexPath i created targets for each answer,
here is my code!
-(void)buttonsClicked:(id)sender
{
UIButton *btn=(UIButton*)sender;
ContestQATableViewCell * cell=(ContestQATableViewCell *)[btn.superview superview];
if (cell.answer1.tag==btn.tag)
{
NSLog(#"%ld",(long)btn.tag);
[cell.answer1 setImage:[UIImage imageNamed:#"RadioChecked"] forState:UIControlStateNormal];
}else{
[cell.answer1 setImage:[UIImage imageNamed:#"RadioUnChecked"] forState:UIControlStateNormal];
}
if (cell.answer2.tag==btn.tag)
{
NSLog(#"%ld",(long)btn.tag);
[cell.answer2 setImage:[UIImage imageNamed:#"RadioChecked"] forState:UIControlStateNormal];
}else{
[cell.answer2 setImage:[UIImage imageNamed:#"RadioUnChecked"] forState:UIControlStateNormal];
}
if (cell.answer3.tag==btn.tag)
{
NSLog(#"%ld",(long)btn.tag);
[cell.answer3 setImage:[UIImage imageNamed:#"RadioChecked"] forState:UIControlStateNormal];
}else{
[cell.answer3 setImage:[UIImage imageNamed:#"RadioUnChecked"] forState:UIControlStateNormal];
}
if (cell.answer4.tag==btn.tag)
{
NSLog(#"%ld",(long)btn.tag);
[cell.answer4 setImage:[UIImage imageNamed:#"RadioChecked"] forState:UIControlStateNormal];
}else{
[cell.answer4 setImage:[UIImage imageNamed:#"RadioUnChecked"] forState:UIControlStateNormal];
}
}
You need add one key your datasource array and modify that key value depend upon button state on click action. Check below code for your reference
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyCustomTableViewCell *cell = (MyCustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"MyCustomTableViewCell"];
[cell.btnSelect addTarget:self action:#selector(btnSelectClick:) forControlEvents:UIControlEventTouchUpInside];
cell.btnSelect.selected = [[[self.arraySource objectAtIndex:indexPath.row] valueForKey:#"isSleected"] boolValue];
cell.btnSelect.tag = indexPath.row;
return cell;
}
- (void)btnSelectClick:(UIButton *)sender {
sender.selected = !sender.selected;
NSMutableDictionary *dicTemp = [NSMutableDictionary dictionaryWithDictionary:[self.arraySource objectAtIndex:sender.tag]];
[dicTemp setValue:[NSNumber numberWithBool:sender.selected] forKey:#"isSleected"];
[self.arraySource replaceObjectAtIndex:sender.tag withObject:dicTemp];
}
Here is arraySource in view did load
self.arraySource = [NSMutableArray array];
[self.arraySource addObject:#{#"name":#"name-1"}];
[self.arraySource addObject:#{#"name":#"name-2"}];
[self.arraySource addObject:#{#"name":#"name-3"}];
I have created two radio buttons on one tableview cell.That are options for a question,But when I select them they both are get selected that I don't want,I want to select only one of them but I am not able to do that......Please help me Here is my code for customCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"cell";
customCell *cell = (customCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"customCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
leftBtnclick = [UIButton buttonWithType:UIButtonTypeCustom];
[leftBtnclick setTag:0];
[leftBtnclick setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
[leftBtnclick setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateSelected];
[leftBtnclick setFrame:CGRectMake(50, 120, 30, 30)];
[leftBtnclick addTarget:self action:#selector(leftTickBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:leftBtnclick];
rightBtnclick = [UIButton buttonWithType:UIButtonTypeCustom];
[leftBtnclick setTag:1];
[rightBtnclick setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
[rightBtnclick setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateSelected];
[rightBtnclick setFrame:CGRectMake(180, 120, 30, 30)];
[rightBtnclick addTarget:self action:#selector(rightTickBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:rightBtnclick];
cell.numberLbl.text = [numberArray objectAtIndex:indexPath.row];
return cell;
}
-(void)leftTickBtnClicked:(id)sender
{
if ([leftBtnclick isSelected]) {
[sender setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
else
{
[sender setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
}
}
-(void)rightTickBtnClicked:(id)sender
{
if ([sender isSelected])
{
[sender setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
}
else
{
[sender setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
}
}
Firstly , set leftBtnclick.tag=999 and rightBtnclick.tag=1000.
And then add this code:-
-(void)leftTickBtnClicked:(id)sender
{
UIButton *leftTickBtn=(UIButton *)sender;
leftTickBtn.selected=!leftTickBtn.selected;
for(UIView *vw in [[sender superview]subviews])
{
if([vw isKindOfClass:[UIButton class]] && vw.tag==1000)
{
UIButton *rightTickBtn=(UIButton *)vw;
if(leftTickBtn.selected)
{
rightTickBtn.selected=NO;
}
else
{
rightTickBtn.selected=YES;
}
}
}
}
-(void)rightTickBtnClicked:(id)sender
{
UIButton *rightTickBtn=(UIButton *)sender;
rightTickBtn.selected=!rightTickBtn.selected;
for(UIView *vw in [[sender superview]subviews])
{
if([vw isKindOfClass:[UIButton class]] && vw.tag==999)
{
UIButton *leftTickBtn=(UIButton *)vw;
if(rightTickBtn.selected)
{
leftTickBtn.selected=NO;
}
else
{
leftTickBtn.selected=YES;
}
}
}
}
Add four buttons on custom table cell IBOutlet it,and write this code in cellforrowatindexpath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *TableIdentifier = #"CELL";
questionAireCell *cell = [questionAireTable dequeueReusableCellWithIdentifier:TableIdentifier];
if (cell == nil)
{
NSArray *obj = [[NSBundle mainBundle] loadNibNamed:#"questionAireCell" owner:self options:nil];
cell = [obj objectAtIndex:0];
}
cell.questionText.text=[cell.questionText.text stringByAppendingFormat:#"%ld",indexPath.row+1];
cell.commentText.userInteractionEnabled=YES;
cell.commentText.editable=YES;
cell.commentText.tag = indexPath.row;
cell.commentText.delegate = self;
NSLog(#"Question Count=%ld",questionDescriptionArr.count);
cell.questionText.text = [questionDescriptionArr objectAtIndex:indexPath.row];
NSString *number = [[[NSString alloc]initWithString:[srNumberArr objectAtIndex:indexPath.row]]stringByAppendingString:#"."];
cell.questionNoLbl.text = number;
[cell.rightOptBtn addTarget:self action:#selector(rightOptBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.rightTickBtn addTarget:self action:#selector(rightOptBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.leftOptBtn addTarget:self action:#selector(leftOptBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.leftTickBtn addTarget:self action:#selector(leftOptBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
// [cell.rightOptBtn addTarget:self action:#selector(rightOptBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
// [cell.rightTickBtn addTarget:self action:#selector(rightOptBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
// [cell.leftOptBtn addTarget:self action:#selector(leftOptBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
// [cell.leftTickBtn addTarget:self action:#selector(leftOptBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
if ([[answerArray objectAtIndex:indexPath.row] isEqualToString:#"YES"])
{
[cell.leftTickBtn setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
[cell.rightTickBtn setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
}
else if ([[answerArray objectAtIndex:indexPath.row] isEqualToString:#"NO"])
{
[cell.leftTickBtn setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
[cell.rightTickBtn setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
}
else
{
[cell.leftTickBtn setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
[cell.rightTickBtn setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
}
return cell;
}
-(void)leftOptBtnClicked:(UIButton *)sender
{
NSLog(#"Left btn clicked");
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:questionAireTable];
NSIndexPath *indexPath1 = [questionAireTable indexPathForRowAtPoint:buttonPosition];
[answerArray replaceObjectAtIndex:indexPath1.row withObject:#"YES"];
[questionAireTable reloadData];
NSLog(#"Answer Array: %#",answerArray);
}
-(void)rightOptBtnClicked:(UIButton *)sender
{
NSLog(#"Left btn clicked");
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:questionAireTable];
NSIndexPath *indexPath1 = [questionAireTable indexPathForRowAtPoint:buttonPosition];
[answerArray replaceObjectAtIndex:indexPath1.row withObject:#"NO"];
[questionAireTable reloadData];
NSLog(#"Answer Array: %#",answerArray);
}
I am having a list displaying a name and a button titled as "Follow". When I tap on the button title should change to "UnFollow". If I tap on the button again the title should again change to "Follow". This is working fine, but when I am scrolling the table the title of the other cells are also changing due to cell reuse.
The code is as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"AuthorsListCell";
AuthorsListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
dic_eachAuthor = [[_arr_authors objectAtIndex:indexPath.row] mutableCopy];
cell.lbl_authorName.text = [dic_eachAuthor objectForKey:#"name"];
cell.btn_followOrUnfollow.tag = indexPath.row;
if([dic_eachAuthor valueForKey:#"follow"]){
[cell.btn_followOrUnfollow setTitle:#"UnFollow" forState:UIControlStateNormal];
}
else{
[cell.btn_followOrUnfollow setTitle:#"Follow" forState:UIControlStateNormal];
}
// action button method declarations
[cell.btn_followOrUnfollow addTarget:self action:#selector(followOrUnfollow:) forControlEvents:UIControlEventTouchUpInside];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
-(void)followOrUnfollow:(UIButton *)sender
{
if ([sender.titleLabel.text isEqualToString:#"Follow"]) {
[sender setTitle:#"UnFollow" forState:UIControlStateNormal];
[dic_eachAuthor setValue:#"1" forKey:#"follow"];
}
else if ([sender.titleLabel.text isEqualToString:#"UnFollow"]) {
[sender setTitle:#"Follow" forState:UIControlStateNormal];
[dic_eachAuthor setValue:nil forKey:#"follow"];
}
}
Please suggest something to prevent the cell reuse.
Add this condition in followOrUnfollow in cellForRowAtIndexPath also
if ([sender.titleLabel.text isEqualToString:#"Follow"]) {
[sender setTitle:#"UnFollow" forState:UIControlStateNormal];
}
else if ([sender.titleLabel.text isEqualToString:#"UnFollow"]) {
[sender setTitle:#"Follow" forState:UIControlStateNormal];
}
Store follow/unfollow state information within datasource.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"CellIdentifier";
ListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
cell.lbl_authorName.text = [[_arr_authors objectAtIndex:indexPath.row] objectForKey:#"name"];
cell.btn_followOrUnfollow.tag = indexPath.row;
// action button method declarations
[cell.btn_followOrUnfollow addTarget:self action:#selector(followOrUnfollow:) forControlEvents:UIControlEventTouchUpInside];
NSString *btnTitle = [[_arr_authors objectAtIndex:indexPath.row] objectForKey:#"userFollowUnfollow"];
[sender setTitle:btnTitle forState:UIControlStateNormal];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
-(void)followOrUnfollow:(UIButton *)sender
{
if ([sender.titleLabel.text isEqualToString:#"Follow"]) {
[sender setTitle:#"UnFollow" forState:UIControlStateNormal];
}
else if ([sender.titleLabel.text isEqualToString:#"UnFollow"]) {
[sender setTitle:#"Follow" forState:UIControlStateNormal];
}
[[_arr_authors objectAtIndex:sender.tag] setValue:sender.titleLabel.text forKey:#"userFollowUnfollow"];
}
When you originally populate the table row, you do it from an array here cell.lbl_authorName.text = [[_arr_authors objectAtIndex:indexPath.row] objectForKey:#"name"];. The problem is that you don't populate the follow or unfollow information from this array. So all you are doing is toggling a button and there is no saving of the state of that button. What you need to do is modify the array to have a place to save the follow/unfollow state. Then populate the state in the table cell from this array. Then when you call followOrUnfollow: you need to modify the state in the array.
When the cell gets reused it goes and checks with the original array to populate it, populate the follow from the array and you will be set.
Edited to add some code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"CellIdentifier";
ListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.lbl_authorName.text = [[_arr_authors objectAtIndex:indexPath.row] objectForKey:#"name"];
cell.btn_followOrUnfollow.tag = indexPath.row;
if([[_arr_authors objectAtIndex:indexPath.row] valueForKey:#"follow"]){
[cell.btn_followOrUnfollow setTitle:#"UnFollow" forState:UIControlStateNormal];
else{
[cell.btn_followOrUnfollow setTitle:#"Follow" forState:UIControlStateNormal];
}
// action button method declarations
[cell.btn_followOrUnfollow addTarget:self action:#selector(followOrUnfollow:) forControlEvents:UIControlEventTouchUpInside];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
-(void)followOrUnfollow:(UIButton *)sender
{
if ([sender.titleLabel.text isEqualToString:#"Follow"]) {
[sender setTitle:#"UnFollow" forState:UIControlStateNormal];
[[_arr_authors objectAtIndex:sender.tag] setValue:1 forKey:#"follow"]
}
else if ([sender.titleLabel.text isEqualToString:#"UnFollow"]) {
[sender setTitle:#"Follow" forState:UIControlStateNormal];
[[_arr_authors objectAtIndex:sender.tag] setValue:0 forKey:#"follow"]
}
}
I am not at my normal machine, so the syntax is probably off, but you should get the idea. Also note you will have to add the follow property to _arr_authors
You are missing something here, try this :
-(void)followOrUnfollow:(UIButton *)sender
{
NSDictionary *dict = (NSDictionary *) _arr_authors[sender tag];
if ([[dict objectForKey:#"name"] isEqualToString:#"Follow"])
{
[sender setTitle:#"UnFollow" forState:UIControlStateNormal];
}
else if ([[dict objectForKey:#"name"] isEqualToString:#"UnFollow"])
{
[sender setTitle:#"Follow" forState:UIControlStateNormal];
}
}
You are cells are reused so please implement this method in AuthorsListTableViewCell:
-(void) prepareForReuse{
[super prepareForReuse];
[self.btn_followOrUnfollow setTitle:#"Follow" forState:UIControlStateNormal];
}
// if the cell is reusable (has a reuse identifier), this is called just before the cell is returned from the table view method dequeueReusableCellWithIdentifier:. If you override, you MUST call super.
And set the correct default value to the cell.
I also recommend to implement this way:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"AuthorsListCell";
AuthorsListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
cell.lbl_authorName.text = [dic_eachAuthor objectForKey:#"name"];
cell.btn_followOrUnfollow.tag = indexPath.row;
[cell.btn_followOrUnfollow setTitle:#"Follow forState:UIControlStateNormal];
[cell.btn_followOrUnfollow setTitle:#"UnFollow" forState:UIControlStateSelected];
if([dic_eachAuthor valueForKey:#"follow"]){
cell.btn_followOrUnfollow.selected = YES;
} else{
cell.btn_followorUnfollow.selected = NO;
}
// action button method declarations
[cell.btn_followOrUnfollow addTarget:self action:#selector(followOrUnfollow:) forControlEvents:UIControlEventTouchUpInside];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
-(void)followOrUnfollow:(UIButton *)sender
{
sender.selected = !sender.selected;
if ([sender.titleLabel.text isEqualToString:#"Follow"]) {
[dic_eachAuthor setValue:#"1" forKey:#"follow"];
}
else if ([sender.titleLabel.text isEqualToString:#"UnFollow"]) {
[dic_eachAuthor setValue:nil forKey:#"follow"];
}
}
In AuthorsListTableViewCell
-(void) prepareForReuse{
[super prepareForReuse];
self.btn_followOrUnfollow.selected = NO;
}
I have 1 UITableView has multiple UIButton (named is "Open"button with different tags) in each row, i set tag for them. Now, after click on any button in each row, it'll show a UIView (detailView). On detailView has 1 "Submit"button. I want to when user click on "Submit" button, "Open"button with tag selected is disable. How can i do that? I used this code :
Code to create tableview with mutiple "Open"button:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"%d,%d",indexPath.section,indexPath.row];
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UIButton *market = [UIButton buttonWithType:UIButtonTypeCustom];
[market addTarget:self action:#selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];
[market setTag:3000];
[market setFrame:CGRectMake(200, 6, 30, 30)];
[cell.contentView addSubview:market];
}
marketButton = (UIButton *)[cell.contentView viewWithTag:3000];
[marketButton setTag:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
_tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);
return cell;
}
And code when click on"Open"button:
- (void)marketPressedAction:(id)sender
{
UIButton *button = (UIButton *)sender;
buttontag = button.tag;
NSLog(#"Market button click at row %d",buttontag);
if ([sender isSelected]) {
[sender setImage:[UIImage imageNamed:#"Marketplace.png"] forState:UIControlStateHighlighted];
[sender setSelected:NO];
}
else {
[sender setImage:[UIImage imageNamed:#"MarketplaceSelect.png"] forState:UIControlStateSelected];
[sender setSelected:YES];
}
}
}
If you want your "Open" button to be disabled after you click submit button, you should use delegates
UIButton *btn = (UIButton *)sender;
btn.enabled = NO;
Try this one in button click method
EDITED :
Change you marketPressedAction parameter id to UIButton * and write code of below
-(void)marketPressedAction:(UIButton *)sender
{
[sender setImage:[UIImage imageNamed:#"Marketplace.png"] forState:UIControlStateHighlighted];
[sender setImage:[UIImage imageNamed:#"MarketplaceSelect.png"] forState:UIControlStateSelected];
/// Here set you image ////////////////
[sender setImage:[UIImage imageNamed:#"myCustome.png"] forState:UIControlStateNormal];
[self createMarketPlaceForm]; // call detailView
sender.enabled = NO;
sender.userInteractionEnabled = NO;
}
When the user clicks on a UIButton on tableview ( tableview has mutiple button in each row), another View is shown.
I want to change image for this button after the user has clicked on the Done button of the other UIView. How can I do that? I'm a newbie. Could you please provide some code for me? Thanks in advance.
UPDATE CODE:
Code for tableview :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"%d,%d",indexPath.section,indexPath.row];
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UIButton *market = [UIButton buttonWithType:UIButtonTypeCustom];
[market addTarget:self action:#selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];
[market setTag:3000];
[market setFrame:CGRectMake(200, 6, 30, 30)];
[cell.contentView addSubview:market];
}
for (UIButton *button in [cell subviews]) { // change name of table here
if ([button isKindOfClass:[UIButton class]]) {
button.tag = indexPath.row;
[button setImage:[UIImage imageNamed:#"Marketplace.png"] forState:UIControlStateNormal];
}
}
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
_tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);
return cell;
}
Code for Open button ( button that user click on to show another View)
- (void)marketPressedAction:(id)sender
{
UIButton *button = (UIButton *)sender;
buttontag = button.tag;
NSLog(#"Market button click at row %d",buttontag);
}
And code for Done button:
-(void)submitMarket
{
for (UIButton *button in [_tableView subviews]) { // change name of table here
if ([button isKindOfClass:[UIButton class]]) {
if (button.tag == buttontag) {
[button setBackgroundImage:[UIImage imageNamed:#"MarketplaceSelect.png"] forState:UIControlStateNormal];
}
}
}
}
Try this :
[yourButton setBackgroundImage:someImge forState:UIControlStateNormal];
Code on button click :
savedTag = button.tag;
Code on Done button click :
for (UIButton *button in [table subviews]) { // change name of table here
if ([button isKindOfClass:[UIButton class]]) {
if (button.tag == savedtag) {
[button setBackgroundImage:someImge forState:UIControlStateNormal];
}
}
}
In cellForRowAtIndexPath in place of this : [market setTag:3000]; write [market setTag:indexPath.row];
Try this :
Replace :
marketButton = (UIButton *)[cell.contentView viewWithTag:3000];
[marketButton setTag:indexPath.row];
With this :
for (UIButton *button in [cell subviews]) { // change name of table here
if ([button isKindOfClass:[UIButton class]]) {
button.tag == indexPath.row;
}
}
One more thing : make first line of cellForRowAtIndexPath as :
NSString *CellIdentifier = #"tableCell";
Change your cellForRowAtIndexPath as :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = #"cell";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UIButton *market = [UIButton buttonWithType:UIButtonTypeCustom];
[market addTarget:self action:#selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];
[market setImage:[UIImage imageNamed:#"Marketplace.png"] forState:UIControlStateNormal];
[market setTag:indexPath.row];
[market setFrame:CGRectMake(200, 6, 30, 30)];
[cell.contentView addSubview:market];
}
else {
for (UIButton *button in [cell subviews]) { // change name of table here
if ([button isKindOfClass:[UIButton class]]) {
button.tag = indexPath.row;
}
}
}
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
_tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);
return cell;
}
you can use a delegate or a NSNotificationCenter,
in your view where is Done button, you need create a property
#property (weak, nonatomic) <protocol_name> delegate;
and in your action method for DoneButton, you need to send a message to that delegate
[self.delegate method_name];
and the other view will be set as delegate for this one
`[viewWithDoneButton setDelegate:self];
and you need to implement the delegate method `
You can set another image for selected state of your button and after click on Done button just set selected state your button.
[yourButton setBackgroundImage:someImge forState:UIControlStateSelected];
after Done button:
[yourButton setSelected:YES];
Or if you use before UIControlStateSelected you can also use any other state. Or even users state - UIControlStateApplication