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;
}
Related
I need to set two buttons on each cell with different actions but same frame, on click one will hide other will visible. How can i do this.
Thanks in advance.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell * cell =
[tableHistory dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:SimpleTableIdentifier] autorelease];
}
else
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:SimpleTableIdentifier] autorelease];
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(270.0, 7.0, 30.0, 30.0)];
if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:#"check"])
[button setImage:[UIImage imageNamed:#"right-with-bg.png"] forState:UIControlStateNormal];
else
[button setImage:[UIImage imageNamed:#"tick-bg.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
cell.textLabel.text = [cellDataArray objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [arrayTktcode objectAtIndex:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell addSubview:btnUnScan];
[cell addSubview:btnUScan];
return cell;
}
-(void)buttonClicked:(id)sender
{
CGPoint touchPoint = [sender convertPoint:CGPointZero toView:tableHistory];
NSIndexPath *indexPath = [tableHistory indexPathForRowAtPoint:touchPoint];
UIButton *button = (UIButton *)sender;
if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:#"Uncheck"])
{
[button setImage:[UIImage imageNamed:#"right-with-bg.png"] forState:UIControlStateNormal];
[arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:#"check"];
}
else
{
[button setImage:[UIImage imageNamed:#"tick-bg.png"] forState:UIControlStateNormal];
[arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:#"Uncheck"];
}
}
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];
I have a tableview where I am displaying my values from database in tableview cells. In this tableview cellforrowatindexpath, I have created a button with an image on it and set selector for it. So when I click on the button my selector is called and it changes my image to another image.
But the problem is it does not identifies the indexpath i.e. if 4 rows are present in the tableview and if I click on the first row button, its image should change but problem is 4th row action is performed and its image is changed because it does not get the proper indexpath of the image to change.
This is my cellforrowatindexpath code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [NSString stringWithFormat:#"Cell%d%d", indexPath.section, indexPath.row];
appDelegate = (StopSnoozeAppDelegate*)[[UIApplication sharedApplication]delegate];
cell =(TAlarmCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[TAlarmCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
mimageButton = [UIButton buttonWithType:UIButtonTypeCustom];
mimageButton.frame=CGRectMake(10, 20, 20, 20);
mimageButton.tag = 1;
onButtonView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 50)];
onButtonView.tag = 2;
onButtonView.image = [UIImage imageNamed:#"alarm_ON.png"];
[mimageButton setImage:onButtonView.image forState:UIControlStateNormal];
[cell.contentView addSubview:mimageButton];
[mimageButton addTarget:self action:#selector(changeMapType::) forControlEvents:UIControlEventTouchUpInside];
}
return cell;
}
This is my changemapType code
-(void)changeMapType:(NSIndexPath*)path1:(UIButton*)sender{
appDelegate.changeimagetype =!appDelegate.changeimagetype;
if(appDelegate.changeimagetype == YES)
{
onButtonView.image = [UIImage imageNamed:#"alarm_OF.png"];
[mimageButton setImage:onButtonView.image forState:UIControlStateNormal];
appDelegate.changeimagetype = YES;
}
else
{
onButtonView.image = [UIImage imageNamed:#"alarm_ON.png"];
[mimageButton setImage:onButtonView.image forState:UIControlStateNormal];
appDelegate.changeimagetype = NO;
}
}
Don't break table view cell reuse just to put a different tag on each button. If your cells are the same, use the same reuse identifier.
You can find out the index path of the sender like this, without any need to mess around with tags. It also works for multi section tables.
CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *hitIndex = [self.tableView indexPathForRowAtPoint:hitPoint];
I don't think you can have additional arguments in an #selector like that. What you might have to do is subclass UIButton and add an NSIndexPath property (or even just an int) and in -(void)changeMapType:(MyCustomButton*)sender access the property there.
Also it seems you do not even use the index path in changeMapType so that will need to be changed as well.
EDIT: Is it the button image you are trying to change? In which case you don't need the index path at all, or a subclass. Just use [sender setImage:(UIImage *)image forState:(UIControlState)state].
Use yourTableView for getting the Index Path.. Try something like this.
- (void)buttonAction:(UIButton*)sender {
UITableViewCell *cell = (UITableViewCell*)button.superview;
NSIndexPath *indexPath = [yourTableView indexPathForCell:cell];
NSLog(#"%d",indexPath.row)
}
I think following code might help you..
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIView *myCheckbox = [[UIView alloc] init];
UIButton *myBt1 = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 30, 30)];
if([appDelegate.changeimagetype == YES"]){
[myBt1 setBackgroundImage:[UIImage imageNamed:#"alarm_ON.png"] forState:UIControlStateNormal];
}
else {
[myBt1 setBackgroundImage:[UIImage imageNamed:#"alarm_OF.png"] forState:UIControlStateNormal];
}
[myBt1 addTarget:self action:#selector(btcheckbox:) forControlEvents:UIControlEventTouchDown];
[myBt1 setTag:indexPath.row];
[myCheckbox addSubview:myBt1];
[myBt1 release];
[myCheckbox release];
[cell addsubview:myview];
return cell;
}
-(void) btcheckbox:(id) sender
{
UIButton *currentButton = (UIButton *) sender;
if([[currentButton backgroundImageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:#"alarm_OF.png"]])
{
appDelegate.changeimagetype = YES;
[currentButton setBackgroundImage:[UIImage imageNamed:#"alarm_ON.png"] forState:UIControlStateNormal];
}else if([[currentButton backgroundImageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:#"alarm_ON.png"]])
{
appDelegate.changeimagetype = NO;
[currentButton setBackgroundImage:[UIImage imageNamed:#"alarm_OF.png"] forState:UIControlStateNormal];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [NSString stringWithFormat:#"Cell%d", indexPath.row];
appDelegate = (StopSnoozeAppDelegate*)[[UIApplication sharedApplication]delegate];
cell =(TAlarmCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[TAlarmCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UIButton *mimageButton = [UIButton buttonWithType:UIButtonTypeCustom];
mimageButton.frame=CGRectMake(10, 20, 20, 20);
mimageButton.tag = indexPath.row;
//onButtonView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 50)];
//onButtonView.tag = 2;
// onButtonView.image = [UIImage imageNamed:#"alarm_ON.png"];
[mimageButton setImage:[UIImage imageNamed:#"alarm_ON.png"] forState:UIControlStateNormal];
mimageButton.selected = NO;
[cell.contentView addSubview:mimageButton];
[mimageButton addTarget:self action:#selector(changeMapType:) forControlEvents:UIControlEventTouchUpInside];
}
return cell;
}
-(void)changeMapType:(UIButton*)sender{
if(sender.selected == YES)
{
// onButtonView.image = [UIImage imageNamed:#"alarm_OF.png"];
[sender setImage:[UIImage imageNamed:#"alarm_ON.png"] forState:UIControlStateNormal];
sender.selected = NO;
}
else
{
//onButtonView.image = [UIImage imageNamed:#"alarm_ON.png"];
[sender setImage:[UIImage imageNamed:#"alarm_OF.png"] forState:UIControlStateNormal];
sender.selected = YES;
}
}
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.
Is it possible to check uitableviewcell accessoryview contains image or not?
For example
if([self.tableView cellForRowAtIndexPath:indexPath].accessoryView == [UIImage imageNamed:#"selected.png"] )
Some this like that or with different method.
Thank you
By default accessoryView will be null.
You can check for -
if (self.tableView.accessoryView) {
// An image is present
}
For my app I have created a custom cell and then added the imageView at the contentView
But for your requirement you can do something like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
cell.accessoryView = imageView];
imageView.tag = 3;
[imageView release];
}
NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];
UIImageView *imageView;
for (UIView *subview in subviews)
{
if([subview isKindOfClass:[UIImageView class]] && subview.tag == 3);
imageView = [subview copy];
}
[subviews release];
if([selectedArray containsObject:indexPath])
imageView.image = [UIImage imageNamed:#"Selected.png"];
else
imageView.image = [UIImage imageNamed:#"Unselected.png"];
}
And in this method do the same thing to get the imageView object
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
First:
if([self.tableView cellForRowAtIndexPath:indexPath].accessoryView == [UIImage imageNamed:#"selected.png"] )
this is wrong... acessaryview is not an image, it's UIView.
Check for:
if(nil != [self.tableView cellForRowAtIndexPath:indexPath].accessoryView)
{
// It has got something in it...
}
else
{
//create an accessary view....
}
This is how I implement multiselect tableview with checkboxes in each cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
UIButton *disclosureButton = [UIButton buttonWithType:UIButtonTypeCustom];
[disclosureButton setFont:[UIFont fontWithName:#"Arial-BoldMT" size:12.0]];
[disclosureButton setBackgroundImage:[UIImage imageNamed:#"checkboxoff.png"] forState:UIControlStateNormal];
disclosureButton.tag = 0;
[disclosureButton setFrame:CGRectMake(0,0, 30,30)];
[disclosureButton addTarget:self action:#selector(select:event:) forControlEvents:UIControlEventTouchUpInside];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.textLabel setUserInteractionEnabled:YES];
[cell setImage:[UIImage imageNamed:#"bluemappointer.png"]];
[cell setAccessoryView:disclosureButton];
}
else{
int n = indexPath.row;
if([selectedPlaces objectForKey:indexPath] != nil){
disclosureButton.tag = 1;
[disclosureButton setBackgroundImage:[UIImage imageNamed:#"checkboxon.png"] forState:UIControlStateNormal];
[cell setAccessoryView:disclosureButton];
}
else{
[disclosureButton setBackgroundImage:[UIImage imageNamed:#"checkboxoff.png"] forState:UIControlStateNormal];
disclosureButton.tag = 0;
[cell setAccessoryView:disclosureButton];
}
}
NSString *name = [tableData objectAtIndex:indexPath.row];
[cell.textLabel setText:name];
return cell;
}