In my table View Cell Each Cell Has two Button as, Edit And Cancel, On click Of Edit at The Same time Cancel Button Should Change. My Problem Is When I Try To click Edit Button Another cell cancel button image was changed not on that cell which i clicked?
Here Is My Code----
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"MenuNameCell";
MenuNameCell *cell = (MenuNameCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MenuNameCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
_checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_checkButton setFrame:CGRectMake(232, 13, 25, 28)];
[_checkButton setBackgroundImage:[UIImage imageNamed:#"edit.png"]forState:UIControlStateNormal];
[_checkButton addTarget:self action:#selector(editQuantity:) forControlEvents:UIControlEventTouchUpInside];
//_checkButton.tag = 0;
[cell.contentView addSubview:_checkButton];
// Creating Button For CANCEL Order
_cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_cancelButton setFrame:CGRectMake(265, 13, 25, 28)];
[_cancelButton setBackgroundImage:[UIImage imageNamed:#"cancel.png"] forState:UIControlStateNormal];
[_cancelButton addTarget:self action:#selector(cancelOreder:) forControlEvents:UIControlEventTouchUpInside];
// _cancelButton.tag = 1;
_textFieldQuantity = [[UITextField alloc] initWithFrame:CGRectMake(125,14,42,21)];
_textFieldQuantity.userInteractionEnabled = NO;
[cell addSubview:_cancelButton];
[cell addSubview:_textFieldQuantity];
} else {
cell._nameLabel = (UILabel *)[cell.contentView viewWithTag:0];
cell._amountMenu = (UILabel *)[cell.contentView viewWithTag:1];
}
[_checkButton setTag:indexPath.row];
[_cancelButton setTag:indexPath.row];
cell._nameLabel.text = [_hotel._orderedMenus objectAtIndex:indexPath.row];
cell._amountMenu.text = [[_hotel._menuPrices objectAtIndex:indexPath.row] stringValue];
_textFieldQuantity.text = [[_hotel._selectedQuantity objectAtIndex:indexPath.row] stringValue];
return cell;
}
Thanks In Advance!!
For each cell button, give the tag of the current indexPath.row for ex:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"MenuNameCell";
MenuNameCell *cell = (MenuNameCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MenuNameCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
_checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_checkButton setFrame:CGRectMake(232, 13, 25, 28)];
[_checkButton setBackgroundImage:[UIImage imageNamed:#"edit.png"]forState:UIControlStateNormal];
[_checkButton addTarget:self action:#selector(editQuantity:) forControlEvents:UIControlEventTouchUpInside];
_checkButton.tag = [NSString stringWithFormat:#"5%d",indexPath.row];
[cell.contentView addSubview:_checkButton];
// Creating Button For CANCEL Order
_cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_cancelButton setFrame:CGRectMake(265, 13, 25, 28)];
[_cancelButton setBackgroundImage:[UIImage imageNamed:#"cancel.png"] forState:UIControlStateNormal];
[_cancelButton addTarget:self action:#selector(cancelOreder:) forControlEvents:UIControlEventTouchUpInside];
_cancelButton.tag = [NSString stringWithFormat:#"6%d",indexPath.row];
_textFieldQuantity = [[UITextField alloc] initWithFrame:CGRectMake(125,14,42,21)];
_textFieldQuantity.userInteractionEnabled = NO;
[cell addSubview:_cancelButton];
[cell addSubview:_textFieldQuantity];
} else {
cell._nameLabel = (UILabel *)[cell.contentView viewWithTag:[NSString stringWithFormat:#"5%d",indexPath.row]];
cell._amountMenu = (UILabel *)[cell.contentView viewWithTag:[NSString stringWithFormat:#"6%d",indexPath.row]];
}
cell._nameLabel.text = [_hotel._orderedMenus objectAtIndex:indexPath.row];
cell._amountMenu.text = [[_hotel._menuPrices objectAtIndex:indexPath.row] stringValue];
_textFieldQuantity.text = [[_hotel._selectedQuantity objectAtIndex:indexPath.row] stringValue];
return cell;
}
Related
In my iphone app i am having a button, when I click on the button, download operation starts and i am replacing the button with UIProgressView. This UIProgressView will show the download progress. But once a UIProgressView is added, when I scroll it is disappeared. Here is my code:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UIMenuItemCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
cell = [self getCellContentView:CellIdentifier];
cell.cellitemImage = (UIImageView *)[cell viewWithTag:2];
cell.cellItemButton = (UIButton *)[cell viewWithTag:3];
DataBaseClass *itemObj = [appDelegate.itemArray objectAtIndex:indexPath.row];
NSString *url;
if([itemObj.itemStatus isEqualToString:#"Available"]){
cell.cellItemButton.userInteractionEnabled = YES;
cell.userInteractionEnabled = YES;
[cell.cellItemButton setTitle:#"" forState:UIControlStateNormal];
[cell.cellItemButton setBackgroundImage:[UIImage imageNamed:#"img_normal"] forState:UIControlStateNormal];
[cell.cellItemButton setBackgroundImage:[UIImage imageNamed:#"img_pressed"] forState:UIControlStateHighlighted];
[cell.cellItemButton addTarget:self action:#selector(download) forControlEvents:UIControlEventTouchUpInside];
url = [NSString stringWithFormat:#"%#",itemObj.availableIcon];
}else if([itemObj.itemStatus isEqualToString:#"Downloading"]){
url = [NSString stringWithFormat:#"%#",itemObj.availableIcon];
[cell.contentView addSubview:myprogressView];
cell.cellItemButton.hidden = YES;
}
[cell.cellitemImage setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:#"item01.png"]];
cell.cellItemName.text = [NSString stringWithFormat:#"%#",itemObj.itemName];
return cell;
}
- (void)download{
UIButton *btn = (UIButton *)clickedBtnSender;
UIMenuItemCell *cell = [(UIMenuItemCell *)[[clickedBtnSender superview] superview] retain];
myprogressView = [[[CustomProgressView alloc]initWithFrame:CGRectMake(25, 140, 100, 21)] retain];
myprogressView.tag = selectedTag;
btn.hidden = YES;
for (UIProgressView *currentProgress in cell.contentView.subviews) {
if ([currentProgress isKindOfClass:[UIProgressView class]]){
[currentProgress removeFromSuperview];
}
}
[cell.contentView addSubview:myprogressView];
}
Please help.
EDIT
- (UIMenuItemCell *) getCellContentView:(NSString *)cellIdentifier {
CGRect CellFrame = CGRectMake(0, 0, 150, 60);
CGRect imgFrame = CGRectMake(20, 48, 110, 123);
CGRect btnFrame = CGRectMake(25, 140, 100, 26);
CGRect progressFrame = CGRectMake(25, 140, 100, 21);
UIImageView *itemImg;
UIButton *itemBtn;
UIProgressView *itemProgView;
UIMenuItemCell *cell = [[UIMenuItemCell alloc] init] ;
cell.frame = CellFrame;
//Initialize ImageView
itemImg = [[UIImageView alloc]initWithFrame:imgFrame];
itemImg.tag = 2;
[cell.contentView addSubview:itemImg];
//Initialize Button
itemBtn = [UIButton buttonWithType:UIButtonTypeCustom];
itemBtn.frame = btnFrame;
itemBtn.tag = 3;
itemBtn.titleLabel.textColor = [UIColor blueColor];
itemBtn.titleLabel.font = [UIFont systemFontOfSize:9.0];
[cell.contentView addSubview:itemBtn];
//Initialize ProgressView
itemProgView = [[CustomProgressView alloc]initWithFrame:progressFrame];
itemProgView.tag = 4;
//[cell.contentView addSubview:itemProgView];
return cell;
}
I think you need to have unique cellIdentifier for each cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"Cell%d", indexPath.row];
UIMenuItemCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell = [self getCellContentView:CellIdentifier];
// Some codes here
}
return cell;
}
Additional based on you last edit
Change
UIMenuItemCell *cell = [[UIMenuItemCell alloc] init];
to
UIMenuItemCell *cell = [[UIMenuItemCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier];
Hi I try this code but it not show image on cell where i am wrong i want when i click every cell then i want to display image or when i scroll cell then image not disappear it should be appear on that cell i use this code but it not working pease help me on this.
#interface imageclass : UITableViewController
{
BOOL changeimagetype;
//UIImage *btnImage;
UIButton *mimageButton;
UIImageView *onButtonView;
}
#property (nonatomic,retain)UIImageView *onButtonView;
#property BOOL changeimagetype;
#property (nonatomic,retain)UIButton *mimageButton;
-(void)changeMapType:(id)sender;
#end
#import "imageclass.h"
#implementation imageclass
#synthesize onButtonView,changeimagetype,mimageButton;
-(void)changeMapType:(id)sender
{
changeimagetype =!changeimagetype;
if(changeimagetype == YES)
{
//[typechange setImage:[UIImage imageNamed:#"map.png"] forState:UIControlStateNormal];
//self.myGreatMapView.mapType = MKMapTypeStandard;
onButtonView.image = [UIImage imageNamed:#"alarm_OF.png"];
[mimageButton setImage:onButtonView.image forState:UIControlStateNormal];
//someBarButtonItem.image = [UIImage imageNamed:#"alarm_ON..png"];
//changeimagetype =NO;
}
else
{
//self.myGreatMapView.mapType = MKMapTypeSatellite;
onButtonView.image = [UIImage imageNamed:#"alarm_ON.png"];
[mimageButton setImage:onButtonView.image forState:UIControlStateNormal];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
mimageButton = [UIButton buttonWithType:UIButtonTypeCustom];
mimageButton.frame=CGRectMake(10, 10, 30, 50);
mimageButton.tag = 1;
onButtonView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 50)];
onButtonView.tag = 2;
onButtonView.image = [UIImage imageNamed:#"alarm_ON.png"];
[mimageButton setBackgroundImage:[onButtonView.image stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0] forState:UIControlStateNormal];
[cell.contentView addSubview:mimageButton];
[mimageButton addTarget:self action:#selector(changeMapType:) forControlEvents: UIControlEventTouchUpInside];
[onButtonView release];
// Configure the cell...
return cell;
}
Define you tableview delegate like this.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
mimageButton = [UIButton buttonWithType:UIButtonTypeCustom];
mimageButton.frame=CGRectMake(10, 10, 30, 50);
mimageButton.tag = 1;
onButtonView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 50)];
onButtonView.tag = 2;
onButtonView.image = [UIImage imageNamed:#"alarm_ON.png"];
[mimageButton setBackgroundImage:[onButtonView.image stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0] forState:UIControlStateNormal];
[cell.contentView addSubview:mimageButton];
[mimageButton addTarget:self action:#selector(changeMapType:) forControlEvents: UIControlEventTouchUpInside];
[onButtonView release];
}
// Configure the cell...
return cell;
}
Here is the code:-
Add this in cellForRowAtIndexPath method. And also set flag variable in your target method for togglebutton.
if(flagButton)
{
onButtonView.image = [UIImage imageNamed:#"alarm_ON.png"];
}
else
{
onButtonView.image = [UIImage imageNamed:#"alarm_OFF.png"];
}
I tried this different approach, and it worked.
While configuring the button,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
mimageButton = [UIButton buttonWithType:UIButtonTypeCustom];
mimageButton.frame=CGRectMake(10, 10, 30, 50);
mimageButton.tag = 1;
[tempButton setImage:[UIImage imageNamed:#"alarm_ON.png"] forState:UIControlStateNormal];
[tempButton setImage:[UIImage imageNamed:#"alarm_OF.png"] forState:UIControlStateSelected];
[cell.contentView addSubview:mimageButton];
[mimageButton addTarget:self action:#selector(changeMapType:) forControlEvents: UIControlEventTouchUpInside];
}
// Configure the cell...
return cell;
}
And in changeMapType: method,
-(void)changeMapType:(id)sender{
changeimagetype =!changeimagetype;
sender.selected = changeimagetype;
}
I am making an iphone application. In one of the view of the application I have implemented the table view. On each table view cell I have put one or two buttons. Now I want that whenever I click the button it give me the value of cell. How can I do it if anybody has an idea about this please let me know.
The code which I have implemented to display the button is:
- (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];
}
cell.textLabel.text = [rangetime objectAtIndex:indexPath.row];
NSString *checkstatus = [finalstatusarray objectAtIndex:indexPath.row];
if ([checkstatus isEqualToString:#"YES" ])
{
UIButton *submitbutton1 = [UIButton buttonWithType:UIButtonTypeCustom];
submitbutton1.tag=1;
submitbutton1.frame = CGRectMake(200, 4, 28, 29);
UIImage * btnImage1 = [UIImage imageNamed:#"yes.png"];
[submitbutton1 setImage:btnImage1 forState:UIControlStateNormal];
cell.accessoryView = submitbutton1;
[submitbutton1 addTarget:self action:#selector(updatestatus) forControlEvents:UIControlEventTouchUpInside];
}
else if ([checkstatus isEqualToString:#"NO" ])
{
UIButton *submitbutton2 = [UIButton buttonWithType:UIButtonTypeCustom];
submitbutton2.tag = 2;
submitbutton2.frame = CGRectMake(200, 4, 28, 29);
UIImage * btnImage1 = [UIImage imageNamed:#"no.png"];
[submitbutton2 setImage:btnImage1 forState:UIControlStateNormal];
[submitbutton2 addTarget:self action:#selector(updatestatus) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = submitbutton2;
}
else if ([checkstatus isEqualToString:#"s" ])
{
UIButton *submitbutton1 = [UIButton buttonWithType:UIButtonTypeCustom];
submitbutton1.tag = 1;
submitbutton1.frame = CGRectMake(255, 5, 28, 29);
UIImage * btnImage1 = [UIImage imageNamed:#"no.png"];
[submitbutton1 setImage:btnImage1 forState:UIControlStateNormal];
[cell addSubview:submitbutton1];// = submitbutton1;
[submitbutton1 addTarget:self action:#selector(updatestatus) forControlEvents:UIControlEventTouchUpInside];
UIButton *submitbutton2 = [UIButton buttonWithType:UIButtonTypeCustom];
submitbutton2.tag = 2;
submitbutton2.frame = CGRectMake(285, 5, 28, 29);
UIImage * btnImage2 = [UIImage imageNamed:#"yes.png"];
[submitbutton2 setImage:btnImage2 forState:UIControlStateNormal];
[cell addSubview:submitbutton2];
[submitbutton2 addTarget:self action:#selector(updatestatus) forControlEvents:UIControlEventTouchUpInside];
}
return cell;
}
Thanku very much.
You can do with the help of following code.
I had edited your answer in target method of UIButton and added event as argument so you can get indexPath of the row.
- (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];
}
cell.textLabel.text = [rangetime objectAtIndex:indexPath.row];
NSString *checkstatus = [finalstatusarray objectAtIndex:indexPath.row];
if ([checkstatus isEqualToString:#"YES" ])
{
UIButton *submitbutton1 = [UIButton buttonWithType:UIButtonTypeCustom];
submitbutton1.tag=1;
submitbutton1.frame = CGRectMake(200, 4, 28, 29);
UIImage * btnImage1 = [UIImage imageNamed:#"yes.png"];
[submitbutton1 setImage:btnImage1 forState:UIControlStateNormal];
cell.accessoryView = submitbutton1;
[submitbutton1 addTarget:self action:#selector(updatestatus:event:) forControlEvents:UIControlEventTouchUpInside];
}
else if ([checkstatus isEqualToString:#"NO" ])
{
UIButton *submitbutton2 = [UIButton buttonWithType:UIButtonTypeCustom];
submitbutton2.tag = 2;
submitbutton2.frame = CGRectMake(200, 4, 28, 29);
UIImage * btnImage1 = [UIImage imageNamed:#"no.png"];
[submitbutton2 setImage:btnImage1 forState:UIControlStateNormal];
[submitbutton2 addTarget:self action:#selector(updatestatus:event:) forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = submitbutton2;
}
else if ([checkstatus isEqualToString:#"s" ])
{
UIButton *submitbutton1 = [UIButton buttonWithType:UIButtonTypeCustom];
submitbutton1.tag = 1;
submitbutton1.frame = CGRectMake(255, 5, 28, 29);
UIImage * btnImage1 = [UIImage imageNamed:#"no.png"];
[submitbutton1 setImage:btnImage1 forState:UIControlStateNormal];
[cell addSubview:submitbutton1];// = submitbutton1;
[submitbutton1 addTarget:self action:#selector(updatestatus:event:) forControlEvents:UIControlEventTouchUpInside];
UIButton *submitbutton2 = [UIButton buttonWithType:UIButtonTypeCustom];
submitbutton2.tag = 2;
submitbutton2.frame = CGRectMake(285, 5, 28, 29);
UIImage * btnImage2 = [UIImage imageNamed:#"yes.png"];
[submitbutton2 setImage:btnImage2 forState:UIControlStateNormal];
[cell addSubview:submitbutton2];
[submitbutton2 addTarget:self action:#selector(updatestatus:event:) forControlEvents:UIControlEventTouchUpInside];
}
return cell;
}
- (void)updatestatus:(id)sender event:(UIEvent *)event
{
NSIndexPath *indexPath = [tblCityList indexPathForRowAtPoint:[[[event
touchesForView:sender] anyObject] locationInView:YourTableName]];
**Now You can access the row value with the help of indexPath.row**
}
First of all, you have to modify the below code...
[submitbutton1 addTarget:self action:#selector(updatestatus:) forControlEvents:UIControlEventTouchUpInside];
Instead of,
[submitbutton1 addTarget:self action:#selector(updatestatus) forControlEvents:UIControlEventTouchUpInside];
And write the following code in updatestatus:
- (IBAction) updatestatus:(id)sender
{
UIButton *btn = (UIButton*) sender;
UITableViewCell *cellSelected = (UITableViewCell*) [btn superview];
NSIndexPath *idxPath = [tableView indexPathForCell:cell];
// Your code...
}
Have the following code
-(void)showCellValue:(UIControl *)button{
UITableViewCell *cell = (UITableViewCell*)button.superview;
NSIndexPath *indexPath =[aTableView indexPathForCell:cell];
NSLog(#"The cell value is %#",[tableData objectAtIndex:indexPath.row]);
}
Assign this method showCellValue to the selector of the button. tableData is the array from which u load the data to the table. And in ur case it s rangeTime array
Hope this helps.
I am able to create a tableview with textlabel and with detaillabeltext.
In addition to that, is it possible to add UIButton into the cell.
I mean after the detaillabeltext, can i place a UIButton ( like "Remove" button )
Currently i have the following code
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//-----------------------------------------------------------------------------------------------------
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
profileName = [appDelegate.sentItemsList objectAtIndex:indexPath.row];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
NSString *subjectData = [profileName.sent_subject stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
cell.textLabel.text = [NSString stringWithFormat: #"%# ", subjectData];
cell.textLabel.font = [UIFont boldSystemFontOfSize:13];
NSString *CompanyName = [profileName.sent_content stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
cell.detailTextLabel.text = [NSString stringWithFormat: #"%# ",CompanyName];
cell.detailTextLabel.font = [UIFont systemFontOfSize:10];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
cell.backgroundColor = [UIColor colorWithRed:230.0/255.0 green:249.0/255.0 blue:230.0/255.0 alpha:2.0];
return cell;
}
Thanks for you time and helps!
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[cell addSubview:button];
You can add button or any other control to the content view of cell.
UIButton *cellButton = [[UIButton alloc]init];
[cellButton addTarget:self action:#selector(uploadData:)forControlEvents:UIControlEventTouchDown];
[cellButton setTitle:#"Upload Now" forState:UIControlStateNormal];
cellButton.frame = CGRectMake(100, 180, 150, 35);
[cell.contentview addSubview:cellButton];
[cellButton release];
Add any control to cell's content view
I have created a check box using the uiimageview and i have placed the checkbox into the uitableview cell like below
i want to get indexpath.row when i check the check box.
so i added the uiimageviiew inside the cell. so the didSelectRowAtIndexPath is gets called and gives me the indexpath.row.
but when the row is selected i want to show the detailed view.
now this runs me into trouble.
so can you people suggest me how to tackle my above problem.
when i check the checkbox i want to get the indexpath.row.
and when the row is selected i need to show the detailed view.
Thanks for your time and help
UPDATE 1 :
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
profileName = [appDelegate.archivedItemsList objectAtIndex:indexPath.row];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"xc"] autorelease];
cb = [[UIButton alloc] initWithFrame:CGRectMake(5,10, unselectedImage.size.width, unselectedImage.size.height)];
[cb setImage:unselectedImage forState:UIControlStateNormal];
[cb setImage:selectedImage forState:UIControlStateSelected];
[cb addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchDown];
[cell.contentView addSubview:cb];
}
if ( tableView == myTableView )
{
titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 0, 150, 35)];
titleLabel.font = [UIFont boldSystemFontOfSize:13];
titleLabel.textColor = [UIColor blackColor];
[cell.contentView addSubview:titleLabel];
NSString *subjectData = [profileName.archive_subject stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[titleLabel setText:[NSString stringWithFormat: #"%# ", subjectData]];
lblDescription = [[UILabel alloc]initWithFrame:CGRectMake(60, 30, 210, 30)];
lblDescription.numberOfLines = 2;
lblDescription.lineBreakMode = YES;
lblDescription.adjustsFontSizeToFitWidth = YES;
lblDescription.font = [UIFont systemFontOfSize:10];
lblDescription.textColor = [UIColor grayColor];
[cell.contentView addSubview:lblDescription];
NSString *CompanyName = [profileName.archive_content stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[lblDescription setText:[NSString stringWithFormat: #"%# ", CompanyName]];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
Use a UIButton instead of UIImageView for your checkbox - this way you can add an action/method to it, where you can grab the indexPath, plus you can add different images for selected/unselected state which will eliminate all the confusing stuff happening in your code above:
So in your cellForRowAtIndexPath: method:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...// Your existing code here
UIImage *unselectedCheckboxImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"unselectedImageName" ofType:#"imageType"]];
UIImage *selectedCheckboxImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"selectedImageName" ofType:#"imageType"]];
UIButton *cb = [[UIButton alloc] initWithFrame:CGRectMake(desiredX, desiredY, unselectedCheckboxImage.frame.size.width, unselectedCheckboxImage.frame.size.height)];
[cb setImage:unselectedCheckboxImage forState:UIControlStateNormal];
[cb setImage:selectedCheckboxImage forState:UIControlStateSelected];
[cb addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchDown];
[cell.contentView addSubview:cb];
[cb release];
}
And then for your button action method:
- (IBAction)buttonAction:(id)sender
{
if ([sender isKindOfClass:[UIButton class]])
{
UIButton *checkboxButton = (UIButton*)sender;
checkboxButton.selected = !checkboxButton.selected;
NSIndexPath *indexPath = [self.myTableView indexPathForCell:(UITableViewCell*)[[checkboxButton superview] superview]];
// Do whatever you like here
}
}
I think your logic is causing the problem in the didSelectRowAtIndexPath: method; make sure that's right. Besides, if you just want to use check mark for the cell I think it's better if you use UITableViewCellAccessoryCheckmark. This may give you a basic idea.