I have trying this task since long time
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
}
long row = [indexPath row];
cell.textLabel.text = items[row];
cell.textLabel.textColor = [UIColor whiteColor];
cell.textLabel.highlightedTextColor = [UIColor blackColor];
cell.detailTextLabel.text = alertsTimeArray[row];
cell.textLabel.frame = CGRectMake(0, 0, 30, 10);
cell.detailTextLabel.textColor = [UIColor whiteColor];
cell.detailTextLabel.highlightedTextColor = [UIColor blackColor];
NSLog(#">> %#",alertsTypeArray);
if (![[alertsTypeArray objectAtIndex:indexPath.row] isEqualToString:#"Critical"]){
NSLog(#">> %#",alertsTypeArray);
UIButton * reset = [UIButton buttonWithType:UIButtonTypeCustom];
[reset addTarget:self
action:#selector(customActionPressedForAlertView:)
forControlEvents:UIControlEventTouchUpInside];
[reset setTitle:#"RESET" forState:UIControlStateNormal];
[reset setTitleColor:[UIColor colorWithRed:0/255.0 green:126/255.0 blue:255/255.0 alpha:1.0] forState:UIControlStateNormal];
reset.frame = CGRectMake(250.0f, 22.0f, 70.0f, 30.0f);
reset.tag = indexPath.row;
[cell addSubview:reset];
}
cell.backgroundColor = [UIColor clearColor];
return cell;
}
(void)customActionPressedForAlertView:(id)sender{
UIButton *mybutton = (UIButton *)sender;
[mybutton removeFromSuperview];
UIAlertView *resetAlert = [[UIAlertView alloc] initWithTitle:#"Are you sure to reset alert?" message:nil delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
[resetAlert show];
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
selectedIndex = indexPath.row;
}
But I'm not able to remove the RESET button from it super view cell Please help me.
Every time my alertsTypeArray will fill with two different alerts, "Critical" and "service". If it is service then I need to add reset button or else I shouldn't , if reset button added and the user clicks the resent button then the row should delete, so for the i deleted the one of the alertsTypeArray type , but the reset button is not removing from the tabel , if i load the table view , reset button come's to critical alert.
Thanks,
As rdelmar stated, you most likely are removing the button, but you are also probably adding on several buttons to the same cell (since you add without checking to see if it exists first).
Simply check if the button already exists and only add it to the cell if it doesn't.
Give your button an arbitrary tag (like 123) and check if that tag exists in the cell before adding it.
So change:
if (![[alertsTypeArray objectAtIndex:indexPath.row] isEqualToString:#"Critical"]){
NSLog(#">> %#",alertsTypeArray);
UIButton * reset = [UIButton buttonWithType:UIButtonTypeCustom];
[reset addTarget:self
action:#selector(customActionPressedForAlertView:)
forControlEvents:UIControlEventTouchUpInside];
[reset setTitle:#"RESET" forState:UIControlStateNormal];
[reset setTitleColor:[UIColor colorWithRed:0/255.0 green:126/255.0 blue:255/255.0 alpha:1.0] forState:UIControlStateNormal];
reset.frame = CGRectMake(250.0f, 22.0f, 70.0f, 30.0f);
reset.tag = indexPath.row;
[cell addSubview:reset];
}
To:
if (![[alertsTypeArray objectAtIndex:indexPath.row] isEqualToString:#"Critical"])
{
NSLog(#">> %#",alertsTypeArray);
UIButton *reset = (UIButton *)[cell.contentView viewWithTag:123];
if (reset == nil)
{
UIButton * reset = [UIButton buttonWithType:UIButtonTypeCustom];
[reset addTarget:self action:#selector(customActionPressedForAlertView:) forControlEvents:UIControlEventTouchUpInside];
[reset setTitle:#"RESET" forState:UIControlStateNormal];
[reset setTitleColor:[UIColor colorWithRed:0/255.0 green:126/255.0 blue:255/255.0 alpha:1.0] forState:UIControlStateNormal];
reset.frame = CGRectMake(250.0f, 22.0f, 70.0f, 30.0f);
reset.tag = 123;
[cell.contentView addSubview:reset];
}
Removing the button should be a bit simpler than you have it.
(void)customActionPressedForAlertView:(UIButton *)sender
{
UIButton *pressedButton = sender;
[pressedButton removeFromSuperview];
}
Related
I'm building a survey app with a table view displaying the questions in rows. Some rows have text fields, some have sliders and some have 5 buttons for the user to select their answers.
I can set the initial UIButton values just fine, with multiple cells of buttons and each button can have their titles set. But when I change the data model and reload the tableview, the first row (0) works but the second row (1) does not. The button1.tag returns 0 for the second row.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell forIndexPath:indexPath];
NSString *subCatKey = [NSString stringWithFormat:#"%ld", indexPath.row];
NSDictionary *thisSubCat = [_subCatListDict objectForKey:subCatKey];
NSString *defaultResponse = #"";
static NSString *CellIdentifier;
CellIdentifier = #"buttonCell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIButton *button1 = [[UIButton alloc] init];
UIButton *button2 = [[UIButton alloc] init];
UIButton *button3 = [[UIButton alloc] init];
UIButton *button4 = [[UIButton alloc] init];
UIButton *button5 = [[UIButton alloc] init];
NSLog(#"Button 1::%ld",(long)button1.tag);
// - 4 = Button range
NSLog(#"Setting up the button cell");
NSArray *buttonResponses = [[NSArray alloc] initWithArray:[thisSubCat objectForKey:#"responses"]];
[buttonResponses objectAtIndex:0];
if(cell == nil )
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSLog(#"Button tag 1::%ld",(long)button1.tag);
if (button1.tag == 0) {
NSLog(#"Setting buttons...");
button1 = (UIButton *)[cell viewWithTag:41];
NSLog(#"Button tag 1::%ld",(long)button1.tag);
button2 = (UIButton *)[cell viewWithTag:42];
button3 = (UIButton *)[cell viewWithTag:43];
button4 = (UIButton *)[cell viewWithTag:44];
button5 = (UIButton *)[cell viewWithTag:45];
[button1 setTitle:[buttonResponses objectAtIndex:0] forState:UIControlStateNormal];
[button2 setTitle:[buttonResponses objectAtIndex:1] forState:UIControlStateNormal];
[button3 setTitle:[buttonResponses objectAtIndex:2] forState:UIControlStateNormal];
[button4 setTitle:[buttonResponses objectAtIndex:3] forState:UIControlStateNormal];
[button5 setTitle:[buttonResponses objectAtIndex:4] forState:UIControlStateNormal];
[button1 addTarget:self
action:#selector(rowButtonSelect:)
forControlEvents:UIControlEventTouchUpInside];
[button2 addTarget:self
action:#selector(rowButtonSelect:)
forControlEvents:UIControlEventTouchUpInside];
[button3 addTarget:self
action:#selector(rowButtonSelect:)
forControlEvents:UIControlEventTouchUpInside];
[button4 addTarget:self
action:#selector(rowButtonSelect:)
forControlEvents:UIControlEventTouchUpInside];
[button5 addTarget:self
action:#selector(rowButtonSelect:)
forControlEvents:UIControlEventTouchUpInside];
}
UILabel *questionLabel = (UILabel *)[cell viewWithTag:301];
questionLabel.text = [thisSubCat objectForKey:#"question"];
UILabel *questionResponses = (UILabel *)[cell viewWithTag:302];
questionResponses.text = defaultResponse;
NSLog(#"Default response:%#",defaultResponse);
return cell;
}
You always Create new Button in cell so it's default tag = 0 so
NSLog(#"Button tag 1::%ld",(long)button1.tag);
will print Button tag 1::0
set
button1.tag indexpath.raw;
and try
I create a tableview has button, this button has been changed image ass different condition. The first load tableview, these buttons set image correct but after condition change, it set image incorrect. Example : If [Selling = 0] , set image "Market.png", else [Selling =2], set image "MarketSelect.png". When value of Selling array changing, i used [_tableview reloadData] but it still not change image. I debug, i see MarketButton is 0x0000. The first load, MarketButton works correct but second load, it is 0x0000. Please give me advice to solve this problem. I tried 2 days but it can not works. thanks much
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (nil == cell) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.button.tag = indexPath.row;
UIButton *market = [UIButton buttonWithType:UIButtonTypeCustom];
[market setFrame:CGRectMake(200, 6, 30, 30)];
market.tag = 4000;
[market addTarget:self action:#selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];
[cell.contentView addSubview:market];
UILabel *pricelabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 0, 80, 30)];
pricelabel.backgroundColor = [UIColor clearColor];
pricelabel.font = [UIFont fontWithName:#"Helvetica" size:16];
pricelabel.font = [UIFont boldSystemFontOfSize:16];
pricelabel.textColor = [UIColor darkGrayColor];
pricelabel.tag = 3000;
//pricelabel.hidden = YES;
pricelabel.textAlignment = NSTextAlignmentRight;
[cell.contentView addSubview: pricelabel];
[pricelabel release];
}
UIButton *marketButton = (UIButton*)[cell.contentView viewWithTag:4000];
[marketButton setTag:indexPath.row];
if([sellingArray count]>0)
{
NSLog(#"sellingArray %#",sellingArray);
if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:#"0"]) // nothing
{
[marketButton setSelected:NO];
[marketButton setImage:[UIImage imageNamed:#"Marketplace.png"] forState:UIControlStateNormal];
marketButton.enabled = YES;
}
else if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:#"2"]) // marketplace
{
[marketButton setSelected:YES];
[marketButton setImage:[UIImage imageNamed:#"MarketplaceSelect.png"] forState:UIControlStateNormal];
marketButton.enabled = YES;
}
}
}
UILabel *pricelbl = (UILabel*)[cell.contentView viewWithTag:3000];
pricelbl.text =[NSString stringWithFormat:#"$%#",[priceNewArray objectAtIndex:indexPath.row]];
if ([sellingArray count]>0) {
if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:#"2"]){
pricelbl.hidden = NO;
}
else if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:#"0"]){
pricelbl.hidden = YES;
}
}
return cell;
}
marketpressAction :
- (void)marketPressedAction:(id)sender
{
buttonPressed = (UIButton *)sender;
buttontag = buttonPressed.tag;
NSLog(#"Market button click at row %d",buttontag);
}
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;
}
I'm a newbie. I am using this code to create a UITableViewCell but when I reload the table the button's image is not always correct, although all labels work fine. I don't know why. How can I fix this issue?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
UILabel *FileNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 30)];
FileNameLabel.tag = 1000;
FileNameLabel.backgroundColor = [UIColor clearColor];
FileNameLabel.font = [UIFont fontWithName:#"Helvetica" size:16];
FileNameLabel.font = [UIFont boldSystemFontOfSize:16];
FileNameLabel.textColor = [UIColor blackColor];
[cell.contentView addSubview: FileNameLabel];
[FileNameLabel release];
UILabel *UploadTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 150, 25)];
UploadTimeLabel.tag = 2000;
UploadTimeLabel.backgroundColor = [UIColor clearColor];
UploadTimeLabel.font = [UIFont fontWithName:#"Helvetica" size:12];
UploadTimeLabel.textColor = [UIColor grayColor];
[cell.contentView addSubview: UploadTimeLabel];
[UploadTimeLabel release];
UILabel *pricelabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 0, 80, 30)];
pricelabel.backgroundColor = [UIColor clearColor];
pricelabel.font = [UIFont fontWithName:#"Helvetica" size:16];
pricelabel.font = [UIFont boldSystemFontOfSize:16];
pricelabel.textColor = [UIColor darkGrayColor];
pricelabel.tag = 3000;
//pricelabel.hidden = YES;
pricelabel.textAlignment = NSTextAlignmentRight;
[cell.contentView addSubview: pricelabel];
[pricelabel release];
market = [[UIButton alloc] init];;
[market setFrame:CGRectMake(200, 6, 30, 30)];
market.tag = 4000;
[market addTarget:self action:#selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];
[cell.contentView addSubview:market];
}
if( [temp count] > 0)
{
UILabel *fileNameLbl = (UILabel*)[cell.contentView viewWithTag:1000];
fileNameLbl.text =[temp objectAtIndex:indexPath.row];
UILabel *uploadlbl = (UILabel*)[cell.contentView viewWithTag:2000];
uploadlbl.text =[UploadTimeAllArr objectAtIndex:indexPath.row];
}
UIButton *marketButton = (UIButton*)[cell.contentView viewWithTag:4000];
[marketButton setTag:indexPath.row];
if([sellingArray count]>0)
{
NSLog(#"sellingArray %#",sellingArray);
if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:#"0"]) // nothing
{
[marketButton setSelected:NO];
[marketButton setImage:[UIImage imageNamed:#"Marketplace.png"] forState:UIControlStateNormal];
marketButton.enabled = YES;
}
else if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:#"2"]) // marketplace
{
[marketButton setSelected:YES];
[marketButton setImage:[UIImage imageNamed:#"MarketplaceSelect.png"] forState:UIControlStateNormal];
marketButton.enabled = YES;
}
}
return cell;
}
Your main problem here is that you are recreating new views in your cell every time this method is called. You want to create all reusable elements inside the if(cell == nil) otherwise it will make duplicates. Anything that is dynamic must be created outside of this. I took your code and modified it. This should work better.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// Everything that does not change should go in here!
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
UILabel *pricelabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 0, 80, 30)];
pricelabel.backgroundColor = [UIColor clearColor];
pricelabel.font = [UIFont fontWithName:#"Helvetica" size:16];
pricelabel.font = [UIFont boldSystemFontOfSize:16];
pricelabel.textColor = [UIColor darkGrayColor];
pricelabel.tag = 3000;
//pricelabel.hidden = YES;
pricelabel.textAlignment = NSTextAlignmentRight;
[cell addSubview:pricelabel];
UIButton *market = [UIButton buttonWithType:UIButtonTypeCustom];
[market setFrame:CGRectMake(200, 6, 30, 30)];
[market addTarget:self action:#selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];
[cell addSubview:market];
}
// find market button, since we could be reusing a cell we cannot rely on a tag
// value to find it. (This would only work with one button though).
UIButton *market;
for (UIView *subview in cell.subviews) {
if ([subview isKindOfClass:[UIButton class]]) {
market = (UIButton *)subview;
break;
}
}
// set all defaults in case of reuse
[market setImage:[UIImage imageNamed:#"DefaultImage.png"] forState:UIControlStateNormal];
market.selected = YES;
market.enabled = NO;
market.clearsContextBeforeDrawing = NO;
if([sellingArray count] > 0) {
NSLog(#"sellingArray %#",sellingArray);
if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:#"0"]) {
// not sure if this is supposed to be YES or NO
market.clearsContextBeforeDrawing = YES;
[market setSelected:NO];
[market setImage:[UIImage imageNamed:#"Marketplace.png"] forState:UIControlStateNormal];
market.enabled = YES;
}
}
[market setTag:indexPath.row];
return cell;
}
Since it appears you are not using ARC, make sure you look over this code for any needed reference counting.
dequeReusablecellWithIdentifier: method get return the cell instance already created available,If the reference points still to nil ,we need a valid cell and create one cell to return from that cellForRowatIndexpath: method.That is what being checked in the (cell ==nil).When you create a new cell it is creation and hence all settings custom and all has to be done here.
Second Edit:
This was copied from an answer above:
inside the -cellForRowAtIndexPath: method:
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellSubtitle];
UIButton *market = [UIButton buttonWithType:UIButtonTypeCustom];
[market setFrame:CGRectMake(200, 6, 30, 30)];
[market addTarget:self action:#selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];
[cell.contentView addSubview:market];
//Add all your UILabel INITIATION stuff here as well
}
UIButton *marketButton;
for (UIView *subview in cell.subviews) {
if ([subview isKindOfClass:[UIButton class]]) {
marketButton = (UIButton *)subview;
break;
}
}
marketButton.tag = [indexPath row];
UILabel *priceLabel = [cell.contentView viewWithTag:3000];
UILabel *uploadTimeLabel = [cell.contentView viewWithTag:2000];
//Set up your labels and button now
return cell;
}
EDIT: Leaving my original answer below for posterity but I see that you are setting the table index row as the MarketButton's tag. If you're using that to figure out which dataSource object to query, this is bad practice. You should be making a custom cell which can hold a reference to the object in your data source, so you don't have to ask the button for its tag, and then ask the data source array for the object at index:tag.
The reason this is bad is because somewhere, the state of your array could change, but the table cell is still displayed and still holds a tag pointing at the wrong index. If you just have the cell keep track of the object in question, no matter what happens to the array structure you're guaranteed to be modifying the object you need to.
The only thing I would change about Firo's answer is to just add a "tag" property to each view in the cell, so you don't have to iterate each time you want to find it.
Also took out the [[UIButton alloc]init] line because it's superfluous and might be considered a dangling pointer.
if (cell == nil) {
// Everything that does not change should go in here!
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
UIButton *market = [UIButton buttonWithType:UIButtonTypeCustom];
[market setFrame:CGRectMake(200, 6, 30, 30)];
[market addTarget:self action:#selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];
market.tag = 9999;
[cell.contentView addSubview:market];
}
//don't have to do UIView iteration here
UIButton *marketButton = [cell.contentView viewWithTag:9999];
i m new, i added this code to customize table view with image and button in row
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ImageOnRightCell";
UILabel *mainLabel, *secondLabel;
UIImageView *photo;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 0.0, 100, 20)];
[button addTarget:self action:#selector(buttonPressedAction:) forControlEvents:UIControlEventTouchUpInside];
[button setTag:1];
[cell.contentView addSubview:button];
photo = [[UIImageView alloc] initWithFrame:CGRectMake(50.0, 0.0, 80.0, 45.0)];
photo.tag = PHOTO_TAG;
photo.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:photo];
}
UIImage *theImage = [UIImage imageNamed:#"man.jpg"];
photo.image = theImage;
return cell;
}
image part coming in row of the table, but not the button .
what is the problem in it please
Try it....
UIButton *finalPriceBtn=[UIButton buttonWithType:UIButtonTypeCustom];
finalPriceBtn.tag=i+200;
finalPriceBtn.frame = CGRectMake(100, 0.0, 100, 20);
[finalPriceBtn addTarget:self action:#selector(goBtnClk:) forControlEvents:UIControlEventTouchUpInside];
finalPriceBtn.titleLabel.font=[UIFont systemFontOfSize:12];
[finalPriceBtn setTitle:[NSString stringWithFormat:#"$%.2f",tempVal] forState:UIControlStateNormal];
[finalPriceBtn setTitleColor:[UIColor colorWithRed: 2.0f/255.0f green: 155.0f/255.0f blue: 213.0f/255.0f alpha:1] forState:UIControlStateSelected];
[finalPriceBtn setTitleColor:[UIColor colorWithRed: 2.0f/255.0f green: 155.0f/255.0f blue: 213.0f/255.0f alpha:1] forState:UIControlStateNormal];
finalPriceBtn.titleLabel.textAlignment=UITextAlignmentLeft;
[cell.contentView addSubview:finalPriceBtn];
Hope i helped.
You are adding the button and then adding the image, therefore the image is on top of the button, do this:
[cell.contentView addSubview:photo];
Before this:
[cell.contentView addSubview:button];
You are adding image above your button so add it below the button.
Use your code like this :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ImageOnRightCell";
UILabel *mainLabel, *secondLabel;
UIImageView *photo;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
photo = [[UIImageView alloc] initWithFrame:CGRectMake(50.0, 0.0, 80.0, 45.0)];
photo.tag = PHOTO_TAG;
photo.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:photo];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100, 0.0, 100, 20);
[button setBackgroundColor:[UIColor redColor]];// Here add some background color to check its visibility
[button addTarget:self action:#selector(buttonPressedAction:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
}
UIImage *theImage = [UIImage imageNamed:#"man.jpg"];
photo.image = theImage;
return cell;
}