Implementing toggle feature in a UIButton - objective-c

I want to know how to add the toggling feature to a UIButton, something like the user taps a unselected button the button becomes selected and stays selected till the user taps it again there by making unselected like it was before.
I was thinking of making an IBAction which changes it from unselected to selected, how can I do that?
Heres what I tried:
-(IBAction)toggle {
//Toggle on implementation.
button.selected = YES;
button.highlighted = NO;
button.enabled = YES;
//Toggle off implementation.
if (button.highlighted == YES) {
button.selected = NO;
button.highlighted = YES;
button.enabled = NO;
}
}
Problem...
-(IBAction)toggleFav {
if (favButton == nil) {
UIImage *unselectedImage = [UIImage imageNamed:#"favUntapped.png"];
UIImage *selectedImage = [UIImage imageNamed:#"favTapped.png"];
[favButton setImage:unselectedImage forState:UIControlStateNormal];
[favButton setImage:selectedImage forState:UIControlStateSelected];
[favButton setFrame:CGRectMake(0, 0, 40, 40)];
}
if([favButton isSelected]){
//Add to menu.
[favButton setSelected:NO];
} else {
//Remove from menu.
[favButton setSelected:YES];
}
}

- (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];
UIImage *unselectedImage = [UIImage imageNamed:#"unselected.png"];
UIImage *selectedImage = [UIImage imageNamed:#"selected.png"];
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
[b setBackgroundImage:unselectedImage forState:UIControlStateNormal];
[b setBackgroundImage:selectedImage forState:UIControlStateSelected];
[b addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[b setFrame:CGRectMake(0, 0, 40, 40)];
[cell.contentView addSubview:b];
}
return cell;
}
-(void) buttonPressed:(UIButton *)sender
{
if([sender isSelected]){
//...
[sender setSelected:NO];
} else {
//...
[sender setSelected:YES];
}
}
Your toggleFav code doesn't make much sense.
if (favButton == nil) { checks, if favButton is present. But if you are wiring it up with IB, it should always been there at that point. And if it wasn't how could the button call this method? So do it like this:
-(void)viewDidLoad
{
//....
UIImage *unselectedImage = [UIImage imageNamed:#"favUntapped.png"];
UIImage *selectedImage = [UIImage imageNamed:#"favTapped.png"];
[favButton setImage:unselectedImage forState:UIControlStateNormal];
[favButton setImage:selectedImage forState:UIControlStateSelected];
[favButton setFrame:CGRectMake(0, 0, 40, 40)];
//....
}
-(IBAction)toggleFav:(UIButton *)sender {
if([sender isSelected]){
//...
[sender setSelected:NO];
} else {
//...
[sender setSelected:YES];
}
}
Here you'll find an example project, with a DetaiView, that holds a Button with the 2 states.
Note: I am saving the information of what button was selected in the NSUserDefaults. You should not do that. Instead you'll want to save it in the model. But as I dont have informations on your model, I am just using NSUserDefaults.

-(void)hitButton:(UIButton*)button
{
buttonOnFlag = !buttonOnFlag;
if( buttonFlag )
[self performSelector:#selector(setHighlight:) withObject:button afterDelay:0];
}
- (void)setHighlight:(UIButton*)button
{
button.highlighted = true;
}

Use button.highlighted property

You should code like this:
-(IBAction)toggle:(id)sender {
//Toggle on implementation.
if (sender.highlighted == NO)
{
sender.selected = YES;
sender.highlighted = NO;
sender.enabled = YES;
}
//Toggle off implementation.
else{
sender.selected = NO;
sender.highlighted = YES;
sender.enabled = NO;
}
}

Related

Reload Section does not handle properly

I have the following implementation where I have title, and two buttons(titles are half and full) on the tablecell. When user selects and close the section and open it again, he only sees the default values of button title which is full rather than his selection.
I could able to see the following method (setHalfButton :indexPath
) is getting called during the reloading, but it does not have any effect.
Code and screenshots are follows.
- (void)selectItemAtIndexPath:(NSIndexPath *)indexPath setQuantity :(double) quantity {
NSMutableArray* array = [selectedRowsInSectionDictionary objectForKey:#(indexPath.section)];
if(array){
[array addObject:indexPath];
} else {
array = [NSMutableArray array];
[array addObject:indexPath];
[selectedRowsInSectionDictionary setObject:array forKey:#(indexPath.section)];
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"ComboCell";
ComboTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[ComboTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
if([selectedRowsInSectionDictionary[#(indexPath.section)] containsObject: indexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
if(
comboItemsArray[indexPath.section].allComboItems[indexPath.row].pQuantity == 0.5)
{
// it comes here after reloading
[self setHalfButton:indexPath];
}
else
{
[self setFullButton:indexPath];
}
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell.halfBtnOutlet.hidden = YES;
cell.fullBtnOutlet.hidden = YES;
}
cell.comboTitle.text =comboItemsArray[indexPath.section].allComboItems[indexPath.row].pName;
[cell.halfBtnOutlet addTarget:self action:#selector(halfBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.fullBtnOutlet addTarget:self action:#selector(fullBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
-(void) setHalfButton : (NSIndexPath*)indexPath
{
ComboTableViewCell* cell = [comboTableView cellForRowAtIndexPath:indexPath];
[cell.halfBtnOutlet setTitleColor:[UIColor colorWithRed:0 green:102.0f/255.0f blue:0 alpha:1] forState:UIControlStateNormal];
[cell.fullBtnOutlet setTitleColor:[UIColor colorWithRed:0.5f green:0.5f blue:0.5f alpha:1] forState:UIControlStateNormal];
[cell.halfBtnOutlet.titleLabel setFont:[UIFont boldSystemFontOfSize:15.f]];
[cell.fullBtnOutlet.titleLabel setFont:[UIFont systemFontOfSize:15.f]];
}
-(void) setFullButton : (NSIndexPath*)indexPath
{
ComboTableViewCell* cell = [comboTableView cellForRowAtIndexPath:indexPath];
[cell.fullBtnOutlet setTitleColor:[UIColor colorWithRed:0 green:102.0f/255.0f blue:0 alpha:1] forState:UIControlStateNormal];
[cell.halfBtnOutlet setTitleColor:[UIColor colorWithRed:0.5f green:0.5f blue:0.5f alpha:1] forState:UIControlStateNormal];
[cell.fullBtnOutlet.titleLabel setFont:[UIFont boldSystemFontOfSize:15.f]];
[cell.halfBtnOutlet.titleLabel setFont:[UIFont systemFontOfSize:15.f]];
}
Your setHalfButton and setFullButton functions are being called from the cellForRowAtIndexPath: datasource method, but they are calling the cellForRowAtIndexPath: tableview method. Since you have not yet returned the cell from the former method, the latter method will return nil to cell, resulting in no visible update.
The setHalfButton and setFullButton methods should be in your ComboTableViewCell class:
-(void) setHalfButton
{
[self.halfBtnOutlet setTitleColor:[UIColor colorWithRed:0 green:102.0f/255.0f blue:0 alpha:1] forState:UIControlStateNormal];
[self.fullBtnOutlet setTitleColor:[UIColor colorWithRed:0.5f green:0.5f blue:0.5f alpha:1] forState:UIControlStateNormal];
[self.halfBtnOutlet.titleLabel setFont:[UIFont boldSystemFontOfSize:15.f]];
[self.fullBtnOutlet.titleLabel setFont:[UIFont systemFontOfSize:15.f]];
}
-(void) setFullButton
{
[self.fullBtnOutlet setTitleColor:[UIColor colorWithRed:0 green:102.0f/255.0f blue:0 alpha:1] forState:UIControlStateNormal];
[self.halfBtnOutlet setTitleColor:[UIColor colorWithRed:0.5f green:0.5f blue:0.5f alpha:1] forState:UIControlStateNormal];
[self.fullBtnOutlet.titleLabel setFont:[UIFont boldSystemFontOfSize:15.f]];
[self.halfBtnOutlet.titleLabel setFont:[UIFont systemFontOfSize:15.f]];
}
Also, you are adding the button action handlers each time you dequeue a cell, but you should only do this when you allocate a new cell. From a design point-of-view these button tap handlers should also be in your ComboTableViewCell class with a delegation pattern to notify the view controller that the half/full was changed.
At the very least it should look something like this:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"ComboCell";
ComboTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[ComboTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
[cell.halfBtnOutlet addTarget:self action:#selector(halfBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.fullBtnOutlet addTarget:self action:#selector(fullBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
}
if([selectedRowsInSectionDictionary[#(indexPath.section)] containsObject: indexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
if(
comboItemsArray[indexPath.section].allComboItems[indexPath.row].pQuantity == 0.5)
{
// it comes here after reloading
[cell setHalfButton];
}
else
{
[cell setFullButton];
}
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell.halfBtnOutlet.hidden = YES;
cell.fullBtnOutlet.hidden = YES;
}
cell.comboTitle.text =comboItemsArray[indexPath.section].allComboItems[indexPath.row].pName;
return cell;
}

accesing contents of uitableview cell in another class

i have a class loginViewController which has table contents. i have loaded a custom cell to each row of the table. in method cellForRowAtIndexPath i have created a button for each row and asigned it a tag value. clicking on this button takes us to new view controller logininsidesViewController. Now in logininsidesViewController class i want to access the button tag i created earlier.what should i do?
loginViewController.m class
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
customcell *cell=(customcell*)[tableView dequeueReusableCellWithIdentifier:#"cellcstm"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(customActionPressed)
forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[UIImage imageNamed:#"add_black.png"] forState:UIControlStateNormal];
button.frame = CGRectMake(275 ,9, 25, 25);
[cell addSubview:button];
if (cell==nil)
{
//[tableView registerNib:[UINib nibWithNibName:#"customcell" bundle:nil] forCellReuseIdentifier:#"cellcstm"];
NSArray *arr12=[[NSBundle mainBundle]loadNibNamed:#"customcell" owner:self options:nil];
cell=[arr12 objectAtIndex:0];
}
for (int i=0; i<=indexPath.row; i++)
{
cell.selectionStyle=UITableViewCellEditingStyleNone;
}
if (indexPath.row==0)
{
cell.lbl.hidden=YES;
UILabel *l=[[UILabel alloc]init];
l.text=#"Login Templates";
l.frame=CGRectMake(100, 15, 180, 28);
[cell.contentView addSubview:l];
}
if (indexPath.row==1)
{
button.tag=1;
cell.img.image=[UIImage imageNamed:#"facebook.png"];
cell.lbl.text=#"Facebook";
[cell.contentView addSubview:button];
}
if (indexPath.row==2)
{
button.tag=2;
cell.img.image=[UIImage imageNamed:#"g+.png"];
cell.lbl.text=#"Google";
[cell.contentView addSubview:button];
}
if (indexPath.row==3)
{
button.tag=3;
cell.img.image=[UIImage imageNamed:#"youtube.png"];
cell.lbl.text=#"Youtube";
[cell.contentView addSubview:button];
}
if (indexPath.row==4)
{
button.tag=4;
cell.img.image=[UIImage imageNamed:#"twitter.png"];
cell.lbl.text=#"Twitter";
[cell.contentView addSubview:button];
}
if (indexPath.row==5)
{
button.tag=5;
cell.img.image=[UIImage imageNamed:#"flicker.png"];
cell.lbl.text=#"Flicker";
[cell.contentView addSubview:button];
}
else
{
cell .textLabel.text= [logarr objectAtIndex:indexPath.row];
}
return cell;
}
i want to access the button.tag in my logininsidesViewController class. plz help
In continuation with Viruss's answer you can create a property with integer type in logininsidesViewController class and pass that from button click.
-(IBAction) customActionPressed:(id)sender{
NSLog(#"button tag:%d",[sender tag]);
logininsidesViewController *objLoginInside = [[logininsidesViewController alloc] initWithNibName:#"logininsidesViewController" bundle:nil];
objLoginInside.buttonTag = sender.tag;
[self.navigationController pushViewController:objLoginInside animated:NO];
}
Add One target method to your buttons and access it with tags,
In cellForRow method Update to this.
[button addTarget:self action:#selector(customActionPressed:) forControlEvents:UIControlEventTouchUpInside];
You can access here with tag,
-(IBAction) customActionPressed:(id)sender{
NSLog(#"button tag:%d",[sender tag]);
}
You are adding your button on cell and again you're adding it on cell's contentView correct that is the problem.
If you want adding it in cell.contentView you must provide allocation in each if condition and then add it
Edit: Do smart coding
viewDidLoad()
[self.myTableView registerNib:[UINib nibWithNibName:#"CViewCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
arImage = #[#"facebook.png", #"g+.png", #"twitter.png", #"flicker.png" ];
arLables = #[#"Facebook", #"GooglePlus", #"Tweeter", #"Flicker"];
cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell * cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(customActionPressed) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[UIImage imageNamed:#"add_black.png"] forState:UIControlStateNormal];
button.frame = CGRectMake(275 ,9, 25, 25);
if (indexPath.row==0)
{
cell.lblText1.hidden=YES;
UILabel *l=[[UILabel alloc]init];
l.text=#"Login Templates";
l.frame=CGRectMake(100, 15, 180, 28);
[cell.contentView addSubview:l];
}
if ( indexPath.row > 0 && indexPath.row <= 5) {
button.tag = indexPath.row;
cell.imageView.image=[UIImage imageNamed:arImage[indexPath.row - 1]];
cell.textLabel.text=[arLables objectAtIndex:indexPath.row - 1];
[cell.contentView addSubview:button];
} else {
cell.textLabel.text= [logarr objectAtIndex:indexPath.row];
}
return cell;
}

how to show data to only visible tableview cells in ios

Hi guys i have a custom tableview cell with a scrollview in it.the problem is all the data is getting loaded in tableview cells at once.what i want is to load the data only for the visible cells in tableView.i used [indexPath forvisiblerows]; it was in vain..cud u guys help me out below is the code.
-(IPadTableViewCell *)getNewCell
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"IpadTableViewCell" owner:nil options:nil];
for (id currentObject in topLevelObjects)
{
if ([currentObject isKindOfClass:[IPadTableViewCell class]])
{
cell= (IPadTableViewCell *)currentObject;
return cell;
}
}
return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// static NSString *CellIdentifier = #"IpadTableViewCell";
//cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
id1=[NSString stringWithFormat:#"%i",indexPath.row];
cell=[dicAlbumCells objectForKey:id1];
if (cell == nil) {
cell=[self getNewCell];
}
cell.tag=indexPath.row;
return cell;
}
-(void)loadImagesForOnscreenRows
{
NSArray *visiblePaths = [tblGallery indexPathsForVisibleRows];
for (NSIndexPath *indexPath in visiblePaths) {
if (imageArray > 0) {
[self imageButtonClicked:nil];
}
}
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
[self loadImagesForOnscreenRows];
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[self loadImagesForOnscreenRows];
}
-(void)imageButtonClicked:(id)sender
{
CGFloat prevButtonXPosition = 18, pageButtonXPosition, pageButtonWidth = 144, pageButtonHeight = 143;
for (NSInteger i =0; i<9; i++) {
pageButtonXPosition = prevButtonXPosition ;
prevButtonXPosition = pageButtonXPosition+pageButtonWidth+20;
UIButton *pageButton = [UIButton buttonWithType:UIButtonTypeCustom];
[pageButton setContentMode:UIViewContentModeScaleAspectFit];
pageButton.layer.borderColor=[[UIColor whiteColor]CGColor];
pageButton.layer.borderWidth = 3;
pageButton.layer.cornerRadius = 0;
[pageButton addTarget:self action:#selector(onClickImage:) forControlEvents:UIControlEventTouchUpInside];
[pageButton setFrame:CGRectMake(pageButtonXPosition,0, pageButtonWidth, pageButtonHeight)];
[pageButton setBackgroundImage:[UIImage imageNamed:[imageArray objectAtIndex:i]] forState:UIControlStateNormal];
[pageButton setTag:i];
[cell.scrollView addSubview:pageButton];
}
UIButton *add=[UIButton buttonWithType:UIButtonTypeCustom];
add.layer.borderColor=[[UIColor whiteColor]CGColor];
add.layer.borderWidth = 3;
add.layer.cornerRadius = 0;
[add addTarget:self action:#selector(onClickAdd:) forControlEvents:UIControlEventTouchUpInside];
[add setFrame:CGRectMake(prevButtonXPosition, 0, 144, 143)];
[add setBackgroundImage:[UIImage imageNamed:#"grey.png"] forState:UIControlStateNormal];
[add setTag:0];
[cell.scrollView addSubview:add];
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(prevButtonXPosition+35,30, 76,76)];
[iv setImage:[UIImage imageNamed:#"last.png"]];
[cell.scrollView addSubview:iv];
[cell.scrollView setContentSize:CGSizeMake(prevButtonXPosition +165, 40)];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell.contentView addSubview:cell.scrollView];
}

Objective C : Adding Action On Array of Buttons

i'm a newbie in Objective-C can someone help me out here? I am trying to make a carousel of button but i failed to do it.
What i got it just displaying all the buttons on load and that's it. I can't add action on each button, I mean tagging them.
It's my first to do declare button in this way.
The buttons are not working.
Here's my code:
- (void)loadView {
[super loadView];
self.view.backgroundColor = [UIColor grayColor];
mainHolder = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"feat.png"]];
[mainHolder setUserInteractionEnabled:YES];
[mainHolder setFrame: CGRectMake(0, 0, 1024, 768)];
[self.view addSubview: mainHolder];
header = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"header.png"]];
[header setFrame:CGRectMake(0, 0, 1024, 50)];
[header setUserInteractionEnabled:YES];
[mainHolder addSubview:header];
// The items to be displayed in the carousel
items = [NSArray arrayWithObjects:
[UIImage imageNamed:#"picOne.png"],
[UIImage imageNamed:#"picTwo.png"],
[UIImage imageNamed:#"picThree.png"],
[UIImage imageNamed:#"picFour.png"],
[UIImage imageNamed:#"picFive.png"],
[UIImage imageNamed:#"picSix.png"],
[UIImage imageNamed:#"picSeven.png"],
[UIImage imageNamed:#"picEight.png"],
[UIImage imageNamed:#"picNine.png"],
[UIImage imageNamed:#"picTen.png"],
[UIImage imageNamed:#"picEleven.png"],
nil];
// Initialize and configure the carousel
carousel = [[iCarousel alloc] initWithFrame:self.view.bounds];
carousel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
carousel.type = iCarouselTypeCoverFlow2;
carousel.dataSource = self;
[self.view addSubview:carousel];
}
#pragma mark -
#pragma mark iCarousel methods
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
return [items count];
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index
{
UIImage *image = [items objectAtIndex:index];
imageChoices = [[[UIButton alloc] initWithFrame:CGRectMake(253, 150, 518, 389)] autorelease];
[imageChoices setBackgroundImage:image forState:UIControlStateNormal];
[imageChoices setUserInteractionEnabled:YES];
[imageChoices setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
imageChoices.titleLabel.font = [imageChoices.titleLabel.font fontWithSize:50];
[imageChoices addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
imageChoices.tag=index;
return imageChoices;
}
- (void)buttonTapped:(UIButton *)sender
{
//I don't know how to tag each button here.
switch(sender.tag){
case 1:{
NSLog(#"Photo1");
}
break;
case 2:{
NSLog(#"Photo2");
}
case 3:{
NSLog(#"Photo3");
}
break;
case 4:{
NSLog(#"Photo4");
}
break;
case 5:{
NSLog(#"Photo5");
}
break;
}
}
Implement the target method this way:
- (IBAction)buttonTapped:(id)sender
{
switch(sender.tag){
case 1:{
NSLog(#"Photo1");
break;
}
case 2:{
NSLog(#"Photo2");
break;
}
case 3:{
NSLog(#"Photo3");
break;
}
case 4:{
NSLog(#"Photo4");
break;
}
case 5:{
NSLog(#"Photo5");
break;
}
default:{
NSLog(#"Default");
break;
}
}
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index
{
UIImage *image = [items objectAtIndex:index];
//Instead of adding single UIButton, add separate for each carousel.
UIButton *imageChoices = [[[UIButton alloc] initWithFrame:CGRectMake(253, 150, 518, 389)] autorelease];
[imageChoices setBackgroundImage:image forState:UIControlStateNormal];
[imageChoices setUserInteractionEnabled:YES];
[imageChoices setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
imageChoices.titleLabel.font = [imageChoices.titleLabel.font fontWithSize:50];
[imageChoices addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
imageChoices.tag=index; //print index, that it should be unique or not?
return imageChoices;
}
Try this
- (void)buttonTapped:(UIButton *)sender
{
UIButton *sendButton = (UIButton *)sender; //No need of this! We already have sender
switch(sendButton.tag)
{
//.......
}
}
add carousel.delegate = self; In your loadview method and then implement
- (void)carousel:(iCarousel *)crsl didSelectItemAtIndex:(NSInteger)index { }

UITableView multiple sections with GUI Elements UIButton, UISwitch, etc

I have been racking my brain on this problem for a day or so now... I've searched this forum as well as google and other blogs etc. All to no avail.
What i'm doing is creating a UITableView with multiple sections and within each section, each row has a different GUI element (i.e. a settings screen)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
and all I'm looking to achieve is that each section to have various GUI elements in various cells. BUT as you can see in this screenshot:
Initially everything appears as it should, but after scrolling UP and DOWN once, the problems begin. Elements begin to shuffle around and the more/longer you scroll UP and DOWN the more the elements move around.
!["UITableView with GUI elements in sections and rows. Bug? Glitch? Ignorance?"][2]
Is this a bug? glitch? or ignorance? (probably the latter) - I've stripped out all other code except for the GUI code and if it means anything I'm creating the UITableView using a NIB
Also, here's the code for my cellForRowAtIndexPath method. I will be eternally grateful if someone could point me in the right direction or tell me what i'm doing wrong.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell
// desired section
if(indexPath.section == 0) {
//
// Load Settings
//
// button
UIButton * loadButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
loadButton.frame = CGRectMake(0.0, 0.0, 300.0, 44.0);
[loadButton setTitle:#"Load Image" forState:UIControlStateNormal];
[loadButton setTitle:#"Load Image" forState:UIControlStateSelected];
[loadButton addTarget:self action:#selector(loadImage:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:loadButton];
} else if(indexPath.section == 1) {
//
// Mode Settings
//
if(indexPath.row == 0) {
cell.text = [mode objectAtIndex:indexPath.row];
} else if(indexPath.row == 1) {
cell.text = [mode objectAtIndex:indexPath.row];
}
} else if(indexPath.section == 2) {
//
// Marker Settings
//
if(indexPath.row == 0) {
// description text
cell.text = [marker objectAtIndex:indexPath.row];
// switch
UISwitch *markerSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
cell.accessoryView = markerSwitch;
[markerSwitch setOn:YES animated:NO];
[markerSwitch addTarget:self action:#selector(markerToggle:) forControlEvents:UIControlEventValueChanged];
[markerSwitch release];
} else if(indexPath.row == 1) {
UISlider *markerGridSlider = [[UISlider alloc] initWithFrame:CGRectMake(0.0, 10.0, 284, 10.0)];
cell.accessoryView = markerGridSlider;
markerGridSlider.minimumValueImage = [UIImage imageNamed:#"size_icon_1x1.png"];
markerGridSlider.maximumValueImage = [UIImage imageNamed:#"size_icon_8x8.png"];
[markerGridSlider setMinimumValue:1.0];
[markerGridSlider setMaximumValue:8.0];
markerGridSlider.value = 8.0;
[markerGridSlider addTarget:self action:#selector(markerGrid:) forControlEvents:UIControlEventValueChanged];
[markerGridSlider release];
} else if(indexPath.row == 2) {
UISlider *markerSpacingSlider = [[UISlider alloc] initWithFrame:CGRectMake(0.0, 10.0, 284, 10.0)];
cell.accessoryView = markerSpacingSlider;
markerSpacingSlider.minimumValueImage = [UIImage imageNamed:#"spacing_icon_min.png"];
markerSpacingSlider.maximumValueImage = [UIImage imageNamed:#"spacing_icon_max.png"];
[markerSpacingSlider setMinimumValue:1.0];
[markerSpacingSlider setMaximumValue:(320.0/8.0)];
markerSpacingSlider.value = 1.0;
[markerSpacingSlider addTarget:self action:#selector(markerSpacing:) forControlEvents:UIControlEventValueChanged];
[markerSpacingSlider release];
}
}
return cell;
}
thanks.
Ken
thanks alon for the help.
maybe i'm not understanding exactly what you mean, but i think i've implemented your suggestions properply. however i still get the glitch of the elements swapping places although now after about 5+ UP and DOWN swipes and my UISwitch is now placed in the top left corner.
here's my updated code:
- (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];
if([indexPath section] == 0) {
//
// Load Settings
//
// button
UIButton *loadButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] autorelease];
loadButton.frame = CGRectMake(0.0, 0.0, 300.0, 44.0);
[loadButton addTarget:self action:#selector(loadImage:) forControlEvents:UIControlEventTouchUpInside];
[loadButton setTag:1000];
[cell.contentView addSubview:loadButton];
}
if([indexPath section] == 1) {
//
// Mode Settings
//
if([indexPath row] == 0) {
cell.text = [mode objectAtIndex:[indexPath row]];
}
if([indexPath row] == 1) {
cell.text = [mode objectAtIndex:[indexPath row]];
}
}
if([indexPath section] == 2) {
//
// Marker Settings
//
if([indexPath row] == 0) {
// switch
UISwitch *markerSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
[markerSwitch setOn:YES animated:NO];
[markerSwitch addTarget:self action:#selector(markerToggle:) forControlEvents:UIControlEventValueChanged];
[markerSwitch setTag:3000];
[cell.contentView addSubview:markerSwitch];
[markerSwitch release];
}
if([indexPath row] == 1) {
// slider
UISlider *markerGridSlider = [[UISlider alloc] initWithFrame:CGRectMake(0.0, 10.0, 284, 10.0)];
markerGridSlider.minimumValueImage = [UIImage imageNamed:#"size_icon_1x1.png"];
markerGridSlider.maximumValueImage = [UIImage imageNamed:#"size_icon_8x8.png"];
[markerGridSlider setMinimumValue:1.0];
[markerGridSlider setMaximumValue:8.0];
[markerGridSlider setValue:8.0];
[markerGridSlider addTarget:self action:#selector(markerGrid:) forControlEvents:UIControlEventValueChanged];
[markerGridSlider setTag:3100];
[cell.contentView addSubview:markerGridSlider];
[markerGridSlider release];
}
if([indexPath row] == 2) {
// slider
UISlider *markerSpacingSlider = [[UISlider alloc] initWithFrame:CGRectMake(0.0, 10.0, 284, 10.0)];
markerSpacingSlider.minimumValueImage = [UIImage imageNamed:#"spacing_icon_min.png"];
markerSpacingSlider.maximumValueImage = [UIImage imageNamed:#"spacing_icon_max.png"];
[markerSpacingSlider setMinimumValue:1.0];
[markerSpacingSlider setMaximumValue:(320.0/8.0)];
[markerSpacingSlider setValue:1.0];
[markerSpacingSlider addTarget:self action:#selector(markerSpacing:) forControlEvents:UIControlEventValueChanged];
[markerSpacingSlider setTag:3200];
[cell.contentView addSubview:markerSpacingSlider];
[markerSpacingSlider release];
}
}
} // end cell == nil
// Load
UIButton *loadButton = (UIButton*)[cell.contentView viewWithTag:1000];
[loadButton setTitle:#"Load Image" forState:UIControlStateNormal];
[loadButton setTitle:#"Load Image" forState:UIControlStateSelected];
// Mode
// Marker
UISwitch *markerSwitch = (UISwitch*)[cell.contentView viewWithTag:3000];
UISlider *markerGridSlider = (UISlider*)[cell.contentView viewWithTag:3100];
UISlider *markerSpacingSlider = (UISlider*)[cell.contentView viewWithTag:3200];
return cell;
}
All the cells bound to a UITable in iOS are reusable.
this is why you first get your cell with the function call "dequeueReusableCellWithIdentifier", the cell might not be nil, pointing to a cell that was loaded before. in your case, even if the cell is not nil, you add subviews to it. this will happen infinite times as you scroll up and down.
Here is a small example:
-(int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString* CellIdentifier = #"Cell";
const NSInteger lblTag = 101;
const NSInteger btnTag = 102;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//First init the cells
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
//Initialize subviews of cells only in this block.
if (indexPath.row == 0) {
//Only the first cell on the top has a UILabel
UILabel *lbl = [[[UILabel alloc] initWithFrame:CGRectMake(5, 5, 100, 20)] autorelease];
lbl.tag = lblTag;
[cell.contentView addSubview:lbl];
} else if (indexPath.row == 1) {
//Only the 2nd cell has a UIButton
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(5, 5, 60, 20);
btn.tag = btnTag;
[cell.contentView addSubview:btn];
}
}
//Modify your cells here
if (indexPath.row == 0) {
//Only the first cell on the top has a UILabel
UILabel *lbl = (UILabel*)[cell.contentView viewWithTag:lblTag];
//Do stuff with our label
lbl.text = [NSString stringWithFormat:#"%d",indexPath.row];
} else if (indexPath.row == 1) {
//Only the 2nd cell has a UIButton
UIButton *btn = (UIButton*)[cell.contentView viewWithTag:btnTag];
//Do stuff with our button
[btn setTitle:[NSString stringWithFormat:#"%d",indexPath.row] forState:UIControlStateNormal];
}
return cell;
}
In your very specific case, you would write this-
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0) {
return 1;
} else if (section == 1) {
return 2;
} else if (section == 2) {
return 3;
} else {
return 0;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
const NSInteger
sec0BtnTag = 101,
markerSwitchTag = 102,
markerGridSliderTag = 103,
markerSpacingSliderTag = 104;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
if([indexPath section] == 0) {
//
// Load Settings
//
// button
UIButton *loadButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] autorelease];
loadButton.frame = CGRectMake(0.0, 0.0, 300.0, 44.0);
[loadButton addTarget:self action:#selector(loadImage:) forControlEvents:UIControlEventTouchUpInside];
[loadButton setTag:sec0BtnTag];
[cell.contentView addSubview:loadButton];
}
else if([indexPath section] == 2) {
//
// Marker Settings
//
if([indexPath row] == 0) {
// switch
UISwitch *markerSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
[markerSwitch setOn:YES animated:NO];
[markerSwitch addTarget:self action:#selector(markerToggle:) forControlEvents:UIControlEventValueChanged];
[markerSwitch setTag:markerSwitchTag];
[cell.contentView addSubview:markerSwitch];
[markerSwitch release];
}
else if([indexPath row] == 1) {
// slider
UISlider *markerGridSlider = [[UISlider alloc] initWithFrame:CGRectMake(0.0, 10.0, 284, 10.0)];
markerGridSlider.minimumValueImage = [UIImage imageNamed:#"size_icon_1x1.png"];
markerGridSlider.maximumValueImage = [UIImage imageNamed:#"size_icon_8x8.png"];
[markerGridSlider setMinimumValue:1.0];
[markerGridSlider setMaximumValue:8.0];
[markerGridSlider setValue:8.0];
[markerGridSlider addTarget:self action:#selector(markerGrid:) forControlEvents:UIControlEventValueChanged];
[markerGridSlider setTag:markerGridSliderTag];
[cell.contentView addSubview:markerGridSlider];
[markerGridSlider release];
}
else if([indexPath row] == 2) {
// slider
UISlider *markerSpacingSlider = [[UISlider alloc] initWithFrame:CGRectMake(0.0, 10.0, 284, 10.0)];
markerSpacingSlider.minimumValueImage = [UIImage imageNamed:#"spacing_icon_min.png"];
markerSpacingSlider.maximumValueImage = [UIImage imageNamed:#"spacing_icon_max.png"];
[markerSpacingSlider setMinimumValue:1.0];
[markerSpacingSlider setMaximumValue:(320.0/8.0)];
[markerSpacingSlider setValue:1.0];
[markerSpacingSlider addTarget:self action:#selector(markerSpacing:) forControlEvents:UIControlEventValueChanged];
[markerSpacingSlider setTag:markerSpacingSliderTag];
[cell.contentView addSubview:markerSpacingSlider];
[markerSpacingSlider release];
}
}
} // end cell == nil
//Load specific cell row values
if([indexPath section] == 0) {
//
// Load Settings
//
// button
UIButton *loadButton = (UIButton*)[cell.contentView viewWithTag:sec0BtnTag];
[loadButton setTitle:#"Load Image" forState:UIControlStateNormal];
[loadButton setTitle:#"Load Image" forState:UIControlStateSelected];
//do other stuff with the button here according to the specific row & section
}
if([indexPath section] == 1) {
//
// Mode Settings
//
if([indexPath row] == 0) {
cell.text = [mode objectAtIndex:[indexPath row]];
}
if([indexPath row] == 1) {
cell.text = [mode objectAtIndex:[indexPath row]];
}
}
if([indexPath section] == 2) {
//
// Marker Settings
//
if([indexPath row] == 0) {
// switch
UISwitch *markerSwitch = (UISwitch*)[cell.contentView viewWithTag:markerSwitchTag];
//do stuff with the switch here according to the specific row & section
}
if([indexPath row] == 1) {
// slider
UISlider *markerGridSlider = (UISlider*)[cell.contentView viewWithTag:markerGridSliderTag];
//do stuff with the slider here according to the specific row & section
}
if([indexPath row] == 2) {
// slider
UISlider *markerSpacingSlider = (UISlider*)[cell.contentView viewWithTag:markerSpacingSliderTag];
//do stuff with the slider here according to the specific row & section
}
}
return cell;
}
change static NSString *CellIdentifier = #"Cell"; for each section like:
NSString *CellIdentifier;
if(indexPath.section==0){ CellIdentifier= #"Cell"; }elseif(indexPath.section==2){ CellIdentifier=#"Cell2"; }