I am new in iOS development, I have a custom table view on which have placed 3 buttons namely,
Add comment
Up arrow
Down arrow
I want to increase the height of cell when down arrow button is pressed, also make add comment button, downarrow button visible.
My code,
#import "JCoutGoingConfess.h"
#interface JCoutGoingConfess (private)
- (BOOL)cellIsSelected:(NSIndexPath *)indexPath;
#end
#implementation JCoutGoingConfess
#define kCellHeight 132.0
#synthesize outConfessionNavbarBAckBtn,JCoutGoingTbl;
//---------------------------- TableView---------------------------------------
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths
withRowAnimation:(UITableViewRowAnimation)animation
{
NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:1 inSection:0];
NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
// If our cell is selected, return double height
if([self cellIsSelected:indexPath]) {
return kCellHeight * 2.0;
}
return kCellHeight;
}
- (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];
}
// ----------------------- cellbackground image-----------------------------
if([self cellIsSelected:indexPath]){
cell.backgroundView =[[UIImageView alloc]initWithFrame:CGRectMake(160, 151, 320, 108)];
cell.backgroundView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:#"inCellFullBg.png"]];
}
// ******** imageView on cell *******
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(112,0, 90, 90)];
imgView.image = [UIImage imageNamed:#"friendsfacepic.png"];
[cell.contentView addSubview:imgView];
//----------------------------Cell buttons-------------------------------------------
if (JcOutCellSize==152)
{
UIButton *AddComment = [UIButton buttonWithType:UIButtonTypeCustom];
[AddComment addTarget:self
action:#selector(TestBtns:)
forControlEvents:UIControlEventTouchDown];
AddComment.frame = CGRectMake(9.0, 128.0, 96.0, 26.0);
[cell.contentView addSubview:AddComment];
UIImage *buttonAddComment = [UIImage imageNamed:#"addcomment.png"];
[AddComment setBackgroundImage:buttonAddComment forState:UIControlStateNormal];
[cell.contentView addSubview:AddComment];
UIButton *UpArrow = [UIButton buttonWithType:UIButtonTypeCustom];
[UpArrow addTarget:self
action:#selector(ResizeCell:)
forControlEvents:UIControlEventTouchDown];
//[DownArrow setTitle:#"Arrow" forState:UIControlStateNormal];
UpArrow.frame = CGRectMake(143.0, 136.0, 26.0, 16.0);
[cell.contentView addSubview:UpArrow];
UIImage *buttonUp = [UIImage imageNamed:#"upArrow.png"];
[UpArrow setBackgroundImage:buttonUp forState:UIControlStateNormal];
[cell.contentView addSubview:UpArrow];
}
//----------------------------------------------------------------------------------
if (JcOutCellSize==132)
{
NSLog(#" down arrow %f",JcOutCellSize);
UIButton *DownArrow = [UIButton buttonWithType:UIButtonTypeCustom];
[DownArrow addTarget:self
action:#selector(IncreseCellHight:)
forControlEvents:UIControlEventTouchDown];
DownArrow.frame = CGRectMake(143.0, 116.0, 26.0, 16.0);
[cell.contentView addSubview:DownArrow];
UIImage *buttondown = [UIImage imageNamed:#"upDownArrow.png"];
[DownArrow setBackgroundImage:buttondown forState:UIControlStateNormal];
[cell.contentView addSubview:DownArrow];
NSLog(#" cell size = %f",JcOutCellSize);
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:TRUE];
// Toggle 'selected' state
BOOL isSelected = ![self cellIsSelected:indexPath];
NSNumber *selectedIndex = [NSNumber numberWithBool:isSelected];
[selectedIndexes setObject:selectedIndex forKey:indexPath];
[JCoutGoingTbl beginUpdates];
[JCoutGoingTbl endUpdates];
}
- (void)viewDidLoad
{
selectedIndexes = [[NSMutableDictionary alloc] init];
[super viewDidLoad];
}
- (void)viewDidUnload
{
selectedIndexes = nil;
[super viewDidUnload];
}
- (BOOL)cellIsSelected:(NSIndexPath *)indexPath {
// Return whether the cell at the specified index path is selected or not
NSNumber *selectedIndex = [selectedIndexes objectForKey:indexPath];
return selectedIndex == nil ? FALSE : [selectedIndex boolValue];
}
This is not working as expected, what could be the problem?
set any integer variable value in button action and put condition in heightForRowAtIndexPath, whatever size you want, and then reload table
Related
Check Images removing after scrolling.
I have to reuse cell and also dont want to use didselectrow method because i am having several buttons in one cell and this is the sample i created for stackoverflow.
I searched a lot in google and found helpless.
Here Is my Sample Code
//
// TVViewController.h
// tableviewcheck
#import <UIKit/UIKit.h>
#interface TVViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{
UITableView *sampleTableView;
UIImageView * imageView1;
UIButton* aButton1;
UIImageView *imageViewchk1;
int tag;
}
#property (nonatomic, strong) IBOutlet UITableView *sampleTableView;
#end
// TVViewController.m
// tableviewcheck
#import "TVViewController.h"
#interface TVViewController ()
#end
#implementation TVViewController
#synthesize sampleTableView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
// Return the number of rows in the section.
// Usually the number of items in your array (the one that holds your list)
// NSLog(#"selected %i and deliveryArray %i",[queueArray count],[deliveryArray count]);
return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//Where we configure the cell in each row
// NSString *CellIdentifier =[NSString stringWithFormat:#"%i-%i", indexPath.section,indexPath.row];
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
imageViewchk1 = [[UIImageView alloc] initWithFrame:CGRectMake(56,0 , 24,24)];
imageViewchk1.image = [UIImage imageNamed:#"check.png"];
imageViewchk1.tag=indexPath.row+10000;
imageViewchk1.hidden=YES;
imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80,80)];
imageView1.image = [UIImage imageNamed:#"newframe.png"];
aButton1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton1 setTag:indexPath.row];
[aButton1 setImage:imageView1.image forState:UIControlStateNormal];
aButton1.frame = CGRectMake(0, 0, 80,80);
[aButton1 addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[aButton1 addSubview:imageViewchk1];
[cell.contentView addSubview:aButton1];
return cell;
}
-(void)buttonClicked:(UIButton*)sender {
tag = sender.tag;
NSLog(#"buttonClicked %i",tag);
UIImageView *imageView = (UIImageView *)[sampleTableView viewWithTag:tag+10000];
if(imageView.hidden)
{
imageView.hidden=NO;
}
else {
imageView.hidden=YES;
}
}
Well, the problem with you implementation is, that you only set the imageView.hidden property in the buttonClicked method.
When a cell is not shown on the screen for a moment and then shown again, cellForRow is called again.
The problem here is, that you do not store in which cell the imageView.hidden has been set to NO. That is why every time a cell is shown again, the cellForRow: sets its imageView.hidden to YES.
So solution will be to store the numbers of the cell, where imageView.hidden= NO and check in cellForRow if the cell at that indexpath is one where imageView.hidden= NO.
Answering My Own Question For the People searching same Functionality, Here How i did the changes
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
tempArray=[[NSMutableArray alloc] init];
for(int count=0;count<50;count++)
[tempArray addObject:[NSNumber numberWithBool:NO]];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
// Return the number of rows in the section.
// Usually the number of items in your array (the one that holds your list)
// NSLog(#"selected %i and deliveryArray %i",[queueArray count],[deliveryArray count]);
return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80,80)];
imageView1.image = [UIImage imageNamed:#"newframe.png"];
imageViewchk1 = [[UIImageView alloc] initWithFrame:CGRectMake(56,0 , 24,24)];
imageViewchk1.image = [UIImage imageNamed:#"check.png"];
imageViewchk1.tag=indexPath.row+10000;
imageViewchk1.hidden=YES;
aButton1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton1 setTag:indexPath.row];
[aButton1 setImage:imageView1.image forState:UIControlStateNormal];
aButton1.frame = CGRectMake(0, 0, 80,80);
[aButton1 addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[aButton1 addSubview:imageViewchk1];
BOOL selected=[[tempArray objectAtIndex:indexPath.row] boolValue];
if(selected)
{
// imageViewchk1.image = [UIImage imageNamed:#"check.png"];
imageViewchk1.hidden=NO;
}
else
{
imageViewchk1.hidden=YES;
}
[cell.contentView addSubview:aButton1];
return cell;
}
-(void)buttonClicked:(UIButton*)sender {
tag = sender.tag;
NSLog(#"buttonClicked %i",tag);
NSIndexPath *index=[sampleTableView indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
BOOL selected=[[tempArray objectAtIndex:index.row] boolValue];
if(selected)
{
[tempArray replaceObjectAtIndex:index.row withObject:[NSNumber numberWithBool:NO]];
}
else
[tempArray replaceObjectAtIndex:index.row withObject:[NSNumber numberWithBool:YES]];
[sampleTableView reloadData];
}
Can i hide my button inside cell when edit mode? My cell populate by array, save in plist. This is my code
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [titleArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"the indexpath.row = %i",indexPath.row);
UITableViewCell *cell = [[UITableViewCell alloc]init];
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(250.0, 25.0, 30.0, 30.0)];
[button setTitle:#"!" forState:UIControlStateNormal];
[button setTitle:#"X" forState:UIControlStateSelected];
[button setTag:indexPath.row + 100];
[button addTarget:self action:#selector(onoffBtn:) forControlEvents:UIControlEventTouchUpInside];
// my label code
UIImageView *bgCell = [[UIImageView alloc]initWithFrame:CGRectMake(0,0 , 320, 80)];
bgCell.image = [UIImage imageNamed:#"BG.jpeg"];
[cell.contentView addSubview:button];
// other cell contentView addSubview everything
return cell;
[tableView reloadData];
}
This is my edit button
- (IBAction)editBtn:(id)sender
{
UIButton *editBtn = (UIButton *)sender;
editBtn.selected = !editBtn.selected;
if (editBtn.selected)
{
button.hidden = YES;
[self.tableView setEditing:YES animated:YES];
}
else
{
button.hidden = NO;
[self.tableView setEditing:NO animated:YES];
}
}
with above code, my button hide in last cell only, just say if i have 3 cells. How do i hide all button hide when in edit mode? i want all button is hiding. Below is my onoffBtn.
- (IBAction)onoffBtn:(id)sender
{
tempIndexPath = [_tableView indexPathForCell:(UITableViewCell*)[[sender superview] superview]];
UITableViewCell *cell = [_tableView cellForRowAtIndexPath:tempIndexPath];
UIButton *onoffBtn = (UIButton *)[cell.contentView viewWithTag:tempIndexPath.row+100];
onoffBtn.selected = !onoffBtn.selected;
if (onoffBtn.selected)
{
// start
}
else
{
// stop
}
}
Please teach me more. Thank you.
You will have to call this method for all the UiTableViewCells of the table:
if (editBtn.selected)
{
for (int row = 0, rowCount = [_iTableView numberOfRowsInSection:0]; row < rowCount; ++row) {
UITableViewCell *cell = [_iTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0]];
UIButton *onoffBtn = (UIButton *)[cell.contentView viewWithTag:tempIndexPath.row+100];
onoffBtn.hidden=YES;
}
}
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;
}
}
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;
}
iam building an application in which iam showing the 5 small images in a single row of the UITable view cell. i want to make selection of row on each image clicked not on the selection of row .
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 160;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"simpsons"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"simpsons"] autorelease];
}
//cell.textLabel.text = [theSimpsons objectAtIndex:indexPath.row];
//NSString *imageName = [NSString stringWithFormat:#"%d.png", indexPath.row];
//cell.imageView.image = [UIImage imageNamed:imageName];
image1= [[UIImageView alloc] initWithFrame:CGRectMake(5.0,10.0,78, 74)];
[cell.contentView addSubview:image1];
image1.image = [UIImage imageNamed:#"0.png"];
image2= [[UIImageView alloc] initWithFrame:CGRectMake(80.0,10.0,78, 74)];
[cell.contentView addSubview:image2];
image2.image = [UIImage imageNamed:#"1.png"];
image3= [[UIImageView alloc] initWithFrame:CGRectMake(170.0,10.0,78, 74)];
[cell.contentView addSubview:image3];
image3.image = [UIImage imageNamed:#"2.png"];
image4= [[UIImageView alloc] initWithFrame:CGRectMake(240.0,10.0,78, 74)];
[cell.contentView addSubview:image4];
image4.image = [UIImage imageNamed:#"3.png"];
return cell;
}
i want on each image selection only a new row will we opened not on row click ?????
Sure.
1: you can custom a cell with a Button(show your image and clickable) and other controls.
Then set the tag of each button in a cell.
2: In the IBAction event of each button, record the tag and use delegate to select relevant row.
Instad of using UIImageView.Use UIButton.In Custom Style..And Place Image In UIButton Background,So Finally Your Image Will Appear And When You Click On Button You TableRow Is Not Selected.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIImage *Image=[UIImage imageNamed:#"QuickSearchImage.png"];
static NSString *identifier = #"Cell";
UIColor *txtCol=[UIColor clearColor];
UIColor *ButtonTextCol=[UIColor whiteColor];
NSString *FontName=#"verdana";
int FontSize=12;
CGRect frame ;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];
for (UIView *subview in subviews) {
[subview removeFromSuperview];
}
[subviews release];
cell=nil;
if(cell == nil)
{
CGRect cellRectangle;
cellRectangle = CGRectMake(0.0, 0.0, 1024, 200);
cell = [[[UITableViewCell alloc] initWithFrame:cellRectangle reuseIdentifier:identifier] autorelease];
UIButton *button ;
frame = CGRectMake(XOFFSET, YOFFSET, COLWIDTH, COLHEIGHT);
button = [[UIButton alloc] initWithFrame:frame];
//[button addTarget:self action:#selector(buttonPressedAction:) forControlEvents:UIControlEventTouchUpInside];
[button setTag:1000];
[cell.contentView addSubview:button];
[button release];
}
}
UIButton *BtnCol = (UIButton *)[cell viewWithTag:COLTAG];
[BtnCol setTag:COLTAG];
[BtnCol setTitle:[ArrCol objectAtIndex:IntIndex] forState:UIControlStateNormal];
[BtnCol setTitleColor:ButtonTextCol forState:UIControlStateNormal];
[BtnCol setBackgroundImage:Image forState:UIControlStateNormal];
BtnCol.titleLabel.font=[UIFont fontWithName:FontName size:FontSize];
return cell;
}