I need to disable/enable uibutton in each row of UItableview, I compare 2 NSMutablearray to enable/disable it.I tried below code but it always enable button in first row. Where's mistake in my code?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *simpleTableIdentifier = [NSString stringWithFormat:#"%d,%d",indexPath.section,indexPath.row];
UITableViewCell *cell= [_tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
UIButton *pausebtn= [UIButton buttonWithType:UIButtonTypeCustom];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier] autorelease];
[pausebtn setFrame:CGRectMake(285, 10, 30,30)];
[pausebtn setTag:4000];
[pausebtn setImage:[UIImage imageNamed:#"Pause.png"] forState:UIControlStateNormal];
[pausebtn addTarget:self action:#selector(pausebtnClicked:event:) forControlEvents:UIControlEventTouchDown];
[cell.contentView addSubview:pausebtn];
}
pausebtn = (UIButton*)[cell.contentView viewWithTag:4000];
[pausebtn addTarget:self action:#selector(pausebtnClicked:event:) forControlEvents:UIControlEventTouchDown];
[pausebtn setImage:[UIImage imageNamed:#"Pause.png"] forState:UIControlStateNormal];
//check 2 arrays
if ([[Array1 objectAtIndex:indexPath.row] integerValue] != [[Array2 objectAtIndex:indexPath.row] integerValue])
{
pausebtn.enabled = YES;
}
else{
pausebtn.enabled = NO;
}
return cell;
}
Thanks for your help.
Declare your UIButton before checking if cell is nil and creating it.
Create UIButton only if cell is nill
If cell already exists, get it by tag.
UIButton *pausebtn;
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier] autorelease];
pausebtn= [UIButton buttonWithType:UIButtonTypeCustom];
.....
}
pausebtn = (UIButton*)[cell.contentView viewWithTag:4000];
.....
Related
I have created UITableview Programmatically and Populated array values dynamically. Here My Problem is, I created a UIButton Programmatically for zeroth row, It created well but When I am Scrolling the tableview button is Populating some other cells also. I don't Know how to solve that issue in my tableview. I want a UIButton on Only Zeroth index of the cell. I have tried the below code..
- (IBAction)MoreBttn:(id)sender {
NSUserDefaults *userDefaults1 = [NSUserDefaults standardUserDefaults];
[userDefaults1 setObject:amenties forKey:#"FACILITIES"];
CGRect screenRect = [[UIScreen mainScreen] bounds];
amentiestableview=[[UITableView alloc]initWithFrame:screenRect style:UITableViewStylePlain];
amentiestableview.delegate=self;
amentiestableview.dataSource=self;
UIEdgeInsets insets = UIEdgeInsetsMake(20,10,20,10);
CGRect tableframe=amentiestableview.frame;
amentiestableview.frame=UIEdgeInsetsInsetRect(tableframe,insets);
[[UIApplication sharedApplication].keyWindow addSubview:amentiestableview];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [amenties count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"facilities";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if(indexPath.row == 0) {
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
float X_Co = self.tableView.frame.size.width - 60;
[b setFrame:CGRectMake(X_Co, 0.0, 60, 45)];
[b setTitle:#"button" forState:UIControlStateNormal];
[b addTarget:self
action:#selector(hidetableview:)
forControlEvents:UIControlEventTouchUpInside];
[b setImage:[UIImage imageNamed:#"clearpink.png"] forState:UIControlStateNormal];
[cell addSubview:b];
}
else{
}
cell.textLabel.text=[amenties objectAtIndex:indexPath.row];
return cell;
}
-(void)hidetableview:(UIButton*)sender
{
amentiestableview.hidden=YES;
}
The cell is being re-used, so the button appear on other cells. Try to update cellForRow method as follows:
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
float X_Co = self.tableView.frame.size.width - 60;
[b setFrame:CGRectMake(X_Co, 0.0, 60, 45)];
[b setTitle:#"button" forState:UIControlStateNormal];
[b addTarget:self
action:#selector(hidetableview:)
forControlEvents:UIControlEventTouchUpInside];
[b setImage:[UIImage imageNamed:#"clearpink.png"] forState:UIControlStateNormal];
[cell addSubview:b];
}
if(indexPath.row == 0) {
b.hidden = false
}
else{
b.hidden = true
}
We are recycling the cells by using the method dequeueReusableCellWithIdentifier. And you should recycle cells because the allocation of views need a lot of resources
Please try the below code
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if(indexPath.row == 0) {
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
float X_Co = self.tableView.frame.size.width - 60;
[b setFrame:CGRectMake(X_Co, 0.0, 60, 45)];
[b setTitle:#"button" forState:UIControlStateNormal];
[b addTarget:self
action:#selector(hidetableview:)
forControlEvents:UIControlEventTouchUpInside];
[b setImage:[UIImage imageNamed:#"clearpink.png"] forState:UIControlStateNormal];
b.tag = 1010
[cell addSubview:b];
}
else{
cell.contentView.viewWithTag(1010).removeFromSuperview()
}
I have a custom table view in my app which has button in each row.
I want to change button background image when flag of that cell is i'1'(check if statement in code.).
it is successfully executing if statements.
Just the button's background image is not changing.
Here is my code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:#"cell"];
cell= [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"]autorelease];
if(tableView == tablePatchFilter)
{
cell.textLabel.text=[filterPatchName objectAtIndex:indexPath.row];
}
else
{
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text=[displayItems objectAtIndex:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UILabel *typeLabel = [[[UILabel alloc]init]autorelease];
typeLabel.backgroundColor = [UIColor clearColor];
typeLabel.text = [displayItemsType objectAtIndex:indexPath.row];
typeLabel.textAlignment = NSTextAlignmentLeft;
typeLabel.font=[UIFont fontWithName:MyFont size:17.f];
typeLabel.frame = CGRectMake(300,2.0f,250,50);
[cell addSubview:typeLabel];
UILabel *typeLabel1 = [[[UILabel alloc]init]autorelease];
typeLabel1.backgroundColor = [UIColor clearColor];
typeLabel1.text = [displayItemPatch objectAtIndex:indexPath.row];
typeLabel1.textAlignment = NSTextAlignmentLeft;
typeLabel1.font=[UIFont fontWithName:MyFont size:17.f];
typeLabel1.frame = CGRectMake(550,2.0f,250,50);
[cell addSubview:typeLabel1];
UILabel *typeLabel2 = [[[UILabel alloc]init]autorelease];
typeLabel2.backgroundColor = [UIColor clearColor];
typeLabel2.font=[UIFont fontWithName:MyFont size:17.f];
typeLabel2.text = [displayItemclass objectAtIndex:indexPath.row];
typeLabel2.textAlignment = NSTextAlignmentLeft;
typeLabel2.frame = CGRectMake(730,2.0f,250,50);
[cell addSubview:typeLabel2];
int selectedSegment = mainSegment.selectedSegmentIndex;
if(selectedSegment == 0)
{
for(int i = 0;i<[displayFlag count];i++)
{ UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.tag=indexPath.row;
[btn addTarget:self
action:#selector(takeSignature:)
forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:btn];
NSMutableString *pathhh= [NSMutableString stringWithFormat:#"%#",[displayFlag objectAtIndex:i]];
NSLog(#"XXXXXXXXXXXXXXXXXXXXXXXXXX%#",pathhh);
if([pathhh isEqualToString:#"1"])
{
NSLog(#"in IFFFFFFF%#",pathhh);
btn.frame = CGRectMake(850,1, 50, 50);
[btn setBackgroundImage:[UIImage imageNamed:#"signature.png"]
forState:UIControlStateNormal];
}
else
{
NSLog(#"in ELSSSSSSSSSS %#",pathhh);
btn.frame = CGRectMake(850,1, 50, 50);
[btn setBackgroundImage:[UIImage imageNamed:#"add.png"]
forState:UIControlStateNormal];
}
}
/*
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(850,1, 50, 50);
[btn setBackgroundImage:[UIImage imageNamed:#"signature.png"]
forState:UIControlStateNormal];
[btn addTarget:self
action:#selector(takeSignature:)
forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:btn];
*/
}
}
cell.textLabel.font=[UIFont fontWithName:MyFont size:17.f];
return cell;
}
In your implementation every time you are creating new cell, after getting cell from "dequeueReusableCellWithIdentifier" you have to check for cell if it nil than only create new cell using "alloc-init".
The better implementation of the particular senireo is to take a custom UITableViewCell.
The following stategie you should follow.
In the custom cell create the outlet for label 1, 2 and 3, than set the test in cellForRowatIndexPath.
Create a property of NSMutableArray in the custom cell class.
After getting the cell from "dequeueReusableCellWithIdentifier" check if cell is nil than only create the buttons and add all the buttons in the NSMutableArray created in step 2.
take the buttons from the NSMutableArray and that apply the condition for setting the images.
Please see the below code:
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(tableView == tablePatchFilter)
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
cell.textLabel.text=[filterPatchName objectAtIndex:indexPath.row];
return cell;
} else {
CustomTableViewCell *cell = (CustomTableViewCell*)[tableView dequeueReusableCellWithIdentifier:#"cell"];
if (!cell) {
cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
int selectedSegment = mainSegment.selectedSegmentIndex;
if(selectedSegment == 0) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.tag=indexPath.row;
[btn addTarget:self action:#selector(takeSignature:) forControlEvents:UIControlEventTouchUpInside];
[cell.buttonArray addObject:btn];
}
}
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text=[displayItems objectAtIndex:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.label1.text = [displayItemsType objectAtIndex:indexPath.row];
cell.label2.text = [displayItemPatch objectAtIndex:indexPath.row];
cell.label3.text = [displayItemclass objectAtIndex:indexPath.row];
for (UIButton *btn in cell.buttonArray) {
NSMutableString *pathhh= [NSMutableString stringWithFormat:#"%#",[displayFlag objectAtIndex:i]];
if([pathhh isEqualToString:#"1"]) {
btn.frame = CGRectMake(850,1, 50, 50);
[btn setBackgroundImage:[UIImage imageNamed:#"signature.png"] forState:UIControlStateNormal];
} else {
btn.frame = CGRectMake(850,1, 50, 50);
[btn setBackgroundImage:[UIImage imageNamed:#"add.png"] forState:UIControlStateNormal];
}
}
return cell;
}
}
I used BVReorderTableView https://github.com/bvogelzang/BVReorderTableView to reorder row in tableview. But i have a problem: When I add UIButton to each row, this button always show clear color.Once selected row, this button just shown.
I tried this code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell;
cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIButton *prioritybtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[prioritybtn setFrame:CGRectMake(220, 0, 70, 32)];
[prioritybtn setTag:4000];
[cell.contentView addSubview:prioritybtn];
}
UIButton *priorityButton = (UIButton*)[cell.contentView viewWithTag:4000];
if ([[items objectAtIndex:indexPath.row] isKindOfClass:[NSString class]] &&
[[items objectAtIndex:indexPath.row] isEqualToString:#"DUMMY"]) {
cell.textLabel.text = #"";
cell.contentView.backgroundColor = [UIColor clearColor];
prioritybtn.hidden= YES;
}
else {
cell.textLabel.text = [items objectAtIndex:indexPath.row];
[priorityButton setSelected:NO];
[priorityButton setTitle:#"Priority" forState:UIControlStateNormal];
priorityButton.enabled = YES;
}
return cell;
}
Also, I want to hide button only at first row, other row is still shown. How can i do that? Thanks
To hide the button at first row:
if (indexPath.row == 1){
prioritybtn.hidden = YES;
prioritybtn.userInteractionEnabled = NO;
} else {
prioritybtn.hidden = NO;
prioritybtn.userInteractionEnabled = YES;
}
Add this before return cell;
Regarding the color, add this where you initialize the button:
UIButton *prioritybtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
prioritybtn.backgroundColor = [UIColor greenColor]; //put the color you want
Try like this:-
cell.selectionStyle =
UITableViewCellSelectionStyleNone;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell;
UIButton *reletedbtn=nil;
UILabel *Nameoflb=nil;
cell = [tableeample dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Nameoflb = [[UILabel alloc] initWithFrame:CGRectMake(0,0,200,40)];
Nameoflb.backgroundColor=[UIColor clearColor];
Nameoflb.textAlignment=NSTextAlignmentCenter;
Nameoflb.textColor=[UIColor blackColor];
[Nameoflb setFont:[UIFont fontWithName:#"Helvetica-Bold" size :14]];
Nameoflb.text=[dataarray objectAtIndex:indexPath.row];
[[cell contentView] addSubview:Nameoflb];
if (indexPath.row!=0)
{
reletedbtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
reletedbtn.frame=CGRectMake(Nameoflb.frame.origin.x+Nameoflb.frame.size.width, Nameoflb.frame.origin.y+10,40, 25);
reletedbtn.tintColor=[UIColor redColor];
[reletedbtn setTitle:#"butoon" forState:UIControlStateNormal];
//[reletedbtn addTarget:self action:#selector(statusMasseage:) forControlEvents:UIControlEventTouchUpInside];
//reletedbtn.tag=1;
[[cell contentView] addSubview:reletedbtn];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
I am using a "Dynamic Prototypes" UITableview for which I am generating the table cell as follows
-(UITableViewCell*)populateFeatureCell:(UITableView*)tableView row:(NSUInteger)row {
NSString *CellIdentifier = #"FeatureCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
SKProduct *product = [STLNavigatorIAPHelper productAtIndex:row];
if (product != nil) {
cell.textLabel.text = [product localizedTitle];
UIButton *buyButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buyButton.frame = CGRectMake(0, 0, 72, 37);
[buyButton setTitle:NSLocalizedString(#"BUY", nil) forState:UIControlStateNormal];
buyButton.tag = row;
[buyButton addTarget:self action:#selector(buyButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.accessoryView = buyButton;
}
[cell setUserInteractionEnabled:YES];
return cell;
}
I am able to see the cell in the table but the cell does not respond to any events. Please note the function buyButtonTapped has been defined as well.
The table row for at index path is getting the first hit.
You can process in there or pass in along to button.
My goal is insert in a cell of grouped tableview a button that is perfectly fill it, like facebook or skype login on iPad. To do this I use this code:
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Login";
UITableViewCell* cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if(indexPath.section == 1 && indexPath.row == 0){
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:cell.frame];
[button setTitle:#"Do Stuff" forState:UIControlStateNormal];
[cell addSubview:button];
}
return cell;
}
But the result is this:
is not what i wanted, the button is wider than cell and its position is incorrect.
I solved doing various tests to find the right values for the frame of the button, but I think this is not the best and most elegant solution. Does anyone have a better solution than mine?
Instead with this code:
if(indexPath.section == 1 && indexPath.row == 0){
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:cell.bounds];//note here bounds
[button setTitle:#"Do Stuff" forState:UIControlStateNormal];
[cell.contentView addSubview:button];
}
return cell;
the result is:
Here is the correct answer. The key is to set the autoresizingMask as shown in following code sample.
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString* CellIdentifier = nil;
if (indexPath.section == BTNSCTN && indexPath.row == BTNROW) {
CellIdentifier = #"ButtonCell";
UITableViewCell* cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier]; // autorelease if not using ARC
UIButton* buttonUp = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[buttonUp setTitle:#"Shake it up!" forState:UIControlStateNormal];
[buttonUp addTarget:self action:#selector(shakePhone)
forControlEvents:UIControlEventTouchUpInside];
buttonUp.frame = cell.contentView.bounds; // or cell.bounds
buttonUp.autoresizingMask =
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:buttonUp];
}
return cell;
}
else
// handle the other sections and cells...
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Login";
UITableViewCell* cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
if(indexPath.section == 1 && indexPath.row == 0){
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];//note here
[button setFrame:cell.bounds];//note here bounds
[button setTitle:#"Do Stuff" forState:UIControlStateNormal];
cell.clipsToBounds=YES;//note here
[cell.contentView addSubview:button];//note here contentview
}
}
return cell;
}