Add subview to UITableViewCell as replacement for cell selection - ios

In my tableview each cell gets a subview by didselectRowAtIndexPath to highlight the current selected row. Everything works fine but in the moment when the tableview was scrolled the subview won't hide from a cell which was selected before.
In short: How do you would make a replacement for "Managing Cell Selection and Highlighting"?
Thanks in advance!
Here is my code:
#import "ViewController.h"
#interface ViewController () <UITableViewDataSource, UITableViewDelegate>
#property (nonatomic,strong) NSArray *tableData;
#end
#implementation ViewController
#synthesize checked_icon;
- (void)viewDidLoad {
[super viewDidLoad];
self.tableData = #[#"A",#"B",#"C",#"D",#"E",#"F",#"G",#"H",#"I",#"J",#"K",#"L",#"M",#"N",#"O",#"P",#"Q",#"R",#"S",#"T"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.tableData count];
}
- (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];
checked_icon = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 44)];
checked_icon.backgroundColor =[UIColor redColor];
}
cell.textLabel.text = self.tableData[indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSIndexPath *currentSelectedIndexPath = [tableView indexPathForSelectedRow];
if (currentSelectedIndexPath != nil)
{
[[tableView cellForRowAtIndexPath:currentSelectedIndexPath] setBackgroundColor: [UIColor whiteColor]];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:currentSelectedIndexPath];
if (cell.isSelected == YES) {
checked_icon.backgroundColor = [UIColor redColor];
}
else {
checked_icon.backgroundColor = [UIColor clearColor];
}
}
return indexPath;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[[tableView cellForRowAtIndexPath:indexPath] setBackgroundColor:[UIColor lightGrayColor]];
UITableViewCell *selectedcell = [tableView cellForRowAtIndexPath:indexPath];
[selectedcell.contentView addSubview:checked_icon];
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (cell.isSelected == YES) {
[cell setBackgroundColor:[UIColor lightGrayColor]];
}
else {
[cell setBackgroundColor:[UIColor whiteColor]];
}
}
#end

You should use 'selectedBackgroundView' property of UITableViewCell, as selection will be handled for you.
EDIT
The proper way to do this would be to create a subclass of UITableViewCell and have a reference to your checkmark in there.
While the code from my original post will work, it's not the best way to do it and will quickly get complicated if your cell view gets more complex.
ORIGINAL
If you don't want to use 'selectedBackgroundView', then this should probably solve your problem:
- (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];
UIView *checked_icon = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 44)];
checked_icon.tag = 1234;
[cell.contentView addSubview:checked_icon];
}
UIView *checked_icon = [cell.contentView viewWithTag:1234];
// Note: color should be set each time a cell is presented
if (cell.isSelected) {
checked_icon.backgroundColor = [UIColor redColor];
}
else {
checked_icon.backgroundColor = [UIColor clearColor];
}
cell.textLabel.text = self.tableData[indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}

Thanks to MrNickBarker!
viewWithTag seems to be the one and only solution...
Here is the code: (not perfect but it works)
#import "ViewController.h"
#interface ViewController () <UITableViewDataSource, UITableViewDelegate>
#property (nonatomic,strong) NSArray *tableData;
#end
#implementation ViewController
#synthesize checked_icon;
- (void)viewDidLoad {
[super viewDidLoad];
self.tableData = #[#"A",#"B",#"C",#"D",#"E",#"F",#"G",#"H",#"I",#"J",#"K",#"L",#"M",#"N",#"O",#"P",#"Q",#"R",#"S",#"T"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [NSString stringWithFormat:#"cell-%d",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
checked_icon = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 44)];
checked_icon.tag = 1234;
[cell.contentView addSubview:checked_icon];
}
cell.textLabel.text = self.tableData[indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSIndexPath *currentSelectedIndexPath = [tableView indexPathForSelectedRow];
if (currentSelectedIndexPath != nil) {
[[tableView cellForRowAtIndexPath:currentSelectedIndexPath] setBackgroundColor: [UIColor whiteColor]];
UITableViewCell *selectedcell = [tableView cellForRowAtIndexPath:currentSelectedIndexPath];
checked_icon = [selectedcell.contentView viewWithTag:1234];
if (selectedcell.isSelected == YES) {
checked_icon.backgroundColor = [UIColor redColor];
}
else {
checked_icon.backgroundColor = [UIColor clearColor];
}
}
checked_icon.backgroundColor = [UIColor clearColor];
return indexPath;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[[tableView cellForRowAtIndexPath:indexPath] setBackgroundColor:[UIColor lightGrayColor]];
UITableViewCell *selectedcell = [tableView cellForRowAtIndexPath:indexPath];
checked_icon = [selectedcell.contentView viewWithTag:1234];
if (selectedcell.isSelected == YES) {
NSLog(#"redColor");
checked_icon.backgroundColor = [UIColor clearColor];
}
else {
NSLog(#"clearColor");
checked_icon.backgroundColor = [UIColor clearColor];
}
checked_icon.backgroundColor = [UIColor redColor];
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
checked_icon = [cell.contentView viewWithTag:1234];
if (cell.isSelected == YES) {
[cell setBackgroundColor:[UIColor lightGrayColor]];
checked_icon.backgroundColor = [UIColor redColor];
}
else {
[cell setBackgroundColor:[UIColor whiteColor]];
checked_icon.backgroundColor = [UIColor clearColor];
}
}
#end

Related

UITableView unresponsive to gestures and not populating cells with data

I created a UITableView programmatically and set all of its attributes, but the data from my NSMutableArray will not populate the cells and the cells do nothing when tapped. Any insight appreciated, thanks!
Clayton
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
list = [[UITableView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height * .3333, self.view.bounds.size.width, self.view.bounds.size.height * .6667) style:UITableViewStylePlain];
list.delegate = self;
list.dataSource = self;
list.backgroundColor = [UIColor magentaColor];
list.scrollEnabled = YES;
list.userInteractionEnabled = YES;
list.multipleTouchEnabled = YES;
list.hidden = NO;
[self.view addSubview:list];
[self.view bringSubviewToFront:list];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [favorites count];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *Cell = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cell];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:Cell];
}
cell.textLabel.text = [[global sharedInstance].favorites objectAtIndex:indexPath.row];
cell.textLabel.textColor = [UIColor blackColor];
cell.userInteractionEnabled = YES;
cell.hidden = NO;
cell.multipleTouchEnabled = YES;
[self.view bringSubviewToFront:cell];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"selected %ld row", (long)indexPath.row);
}
Got it thanks, the problem was with the data transferring from my singleton array of favorites to my local array (which was previously empty). Much obliged :)
At the end of viewDidLoad method, just add this:
[list reloadData];

UITableViewCell override: when alloc UILabel in cellForRowAtIndexPath?

I want to show UITableviewCell from nib file. And also want to add dynamic label on it. But the label is override it self.
Please provide me any method where I can call UITableviewCell from nib and also add dynamic label.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UINib *nib = [UINib nibWithNibName:#"ItemCell" bundle:nil];
[[self tblView] registerNib:nib forCellReuseIdentifier:#"ItemCell"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 150.0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ItemCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ItemCell"];
for (int i=0; i<4; i++) {
UILabel *lbl=[[UILabel alloc] initWithFrame:CGRectMake(0.0, 20.0*i, 150.0, 20.0)];
lbl.text=#"This is custom cell.";
[cell.contentView addSubview:lbl];
}
return cell;
}
Use this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UILabel *lbl;
ItemCell *cell = (ItemCell *)[tableView dequeueReusableCellWithIdentifier:#"ItemCell"];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ItemCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
lbl=[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 150.0, 60.0)];
lbl.tag = indexpath.row;
[cell.contentView addSubview:lbl];
}
lbl = (UIlabel*) [cell.contentView viewWithTag:indexpath.row];
lbl.text=#"This is custom cell.";
return cell;
}
Try this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ItemCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ItemCell"];
UILabel* label = (UILabel*)[cell.containtView viewWithTag:indexPath.row];
if(!label) {
UILabel *lbl=[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 150.0, 60.0)];
lbl.text=#"This is custom cell.";
lbl.tag = indexPath.row
[cell.contentView addSubview:lbl];
}
label.text = #"your text";
return cell;
}

UITableView with multiple sections and accessoryType

This is my functions for my table View.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.categories count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return #"test";
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(#"%d", self.collections.count);
return [self.collections count];
}
- (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];
}
switch ([indexPath section])
{
case 0:
self.myList = self.collections;
break;
}
cell.textLabel.text = self.collections[indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 48;
}
Actually, with this code, my table view show all the section and cells in the same view.
However, I would like a table view which show, at first time, a row with my title of section.
When i click on the row where there is my title of section, i would like to show the cells which are in the section. How can i do that?
Do i need 2 tableViews?
You should have to take two uitableview in that delegate will be same after that you should have to use this code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"%#",indexPath];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==Nil)
{
cell= [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
if (isDeptSelected)
{
//[cell.imageView setImage:[UIImage imageNamed:#"Department.png"]];
cell.textLabel.text = [[arrDept_Detail valueForKey:#"dep_Name"]objectAtIndex:indexPath.row];
}
else if (isEmpSelected)
{
[cell.imageView setImage:[UIImage imageNamed:#"Emp_Images"]];
cell.textLabel.text = [[arrEmp_Detail valueForKey:#"emp_Name"]objectAtIndex:indexPath.row];
}
// cell.textLabel.text=[[arrDept_Detail objectAtIndex:indexPath.row]valueForKeyPath:#"dep_Name"];
[cell setBackgroundColor:[UIColor brownColor]];
[cell.textLabel setTextColor:[UIColor greenColor]];
[cell.textLabel setFont:[UIFont fontWithName:#"Georgia-Bold" size:16]];
cell.textLabel.highlightedTextColor=[UIColor purpleColor];
[self.view setBackgroundColor:[UIColor brownColor]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (isDeptSelected)
{
isDeptSelected=false;
isEmpSelected=true;
[btnDoneclick setHidden:NO];
[self.view addSubview:btnDoneclick];
EmpInDepTableView=[[UITableView alloc]initWithFrame:CGRectMake(160, 284,0, 0)];
[EmpInDepTableView setDelegate:self];
[EmpInDepTableView setDataSource:self];
[EmpInDepTableView setHidden:NO];
[EmpInDepTableView setBackgroundColor:[UIColor brownColor]];
EmpInDepTableView.layer.borderWidth=3.0f;
EmpInDepTableView.layer.cornerRadius=10.0f;
[self.view addSubview:EmpInDepTableView];
self.tabBarController.tabBar.userInteractionEnabled=NO;
UITableViewCell *cell=[Dept_TableView cellForRowAtIndexPath:indexPath];
DeptId=[Dep_Detail fetchDeptId_DeptName:cell.textLabel.text];
arrEmp_Detail = [[Emp_Detail fetchEmp_By_DeptId:DeptId]mutableCopy];
[UITableView animateWithDuration:0.6 animations:^
{
[EmpInDepTableView setFrame:CGRectMake(0, 64,320, 430)];
[btnDoneclick setFrame:CGRectMake(0,490,320,29)];
}completion:^(BOOL finished)
{
[EmpInDepTableView reloadData];
}];
}
else if (isEmpSelected)
{
}
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] init];
[view setBackgroundColor:[UIColor brownColor]];
return view;
}
-(IBAction)btnDoneclicked:(id)sender
{
[EmpInDepTableView reloadData];
isDeptSelected=true;
isEmpSelected=false;
[btnDoneclick setHidden:YES];
self.tabBarController.tabBar.userInteractionEnabled=YES;
[UITableView animateWithDuration:0.6 animations:^
{
[EmpInDepTableView setFrame:CGRectMake(160, 284, 0, 0)];
[btnDoneclick setFrame:CGRectMake(160, 284, 0, 0)];
[self.view setBackgroundColor:[UIColor brownColor]];
}completion:^(BOOL finished)
{
}];
}

How to pass data between two UITableViewControllers

I would like to pass data between two UITableViewControllers. In first UITableViewController I display NewPackageViewController which contains some cells like in code below:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *aCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (indexPath.section ==0)
{
UITextField* textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, 310, 44)];
[textField setPlaceholder:#"Set package number here"];
[textField setEnablesReturnKeyAutomatically: YES];
[textField setReturnKeyType:UIReturnKeyDone];
[textField addTarget:self action:#selector(dismissKeyBoard:) forControlEvents:UIControlEventEditingDidEndOnExit];
[textField setKeyboardType:UIKeyboardTypeNumbersAndPunctuation];
[aCell addSubview:textField];
}
else if(indexPath.section ==1){
aCell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
[aCell setSelectionStyle:UITableViewCellSelectionStyleNone];
[aCell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
aCell.textLabel.text = creationDate;
aCell.detailTextLabel.text = #"Choose";
aCell.imageView.image = [UIImage imageNamed:#"Date.png"] ;
}
else if(indexPath.section ==2)
{
[aCell setSelectionStyle:UITableViewCellSelectionStyleNone];
aCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
aCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
aCell.textLabel.text = self.storeName;
aCell.imageView.image = [UIImage imageNamed:#"Buildings2020.png"];
aCell.detailTextLabel.text = #"Choose";
}
else if(indexPath.section ==4){
aCell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
aCell.textLabel.text = #"Add package item";
[aCell.textLabel setTextColor:[UIColor orangeColor]];
aCell.imageView.image = [UIImage imageNamed:#"New.png"];
[aCell setSelectionStyle:UITableViewCellSelectionStyleNone];
aCell.backgroundColor = [UIColor clearColor];
}
else if(indexPath.section ==5){
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(10, 0, 300, 100)];
[textView setEditable:YES];
[textView setFont:[UIFont fontWithName:#"Verdana" size:14]];
[textView setReturnKeyType:UIReturnKeyDone];
textView.delegate = self;
[aCell setSelectionStyle:UITableViewCellSelectionStyleNone];
[aCell addSubview:textView];
}
return aCell;
}
After clicking on the section == 2 my app properly display second UITableView :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
switch (indexPath.section)
{
case ...:
break;
........
case 2:
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [storyBoard
instantiateViewControllerWithIdentifier:#"SelectStoreView"];
UINavigationController *navController = [[UINavigationController
alloc]initWithRootViewController:vc];
[self.navigationController presentViewController:navController animated:YES
completion:nil];
break; }
default:
break;
In SelectStoreViewController I implemented checkmarks
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return arrayOfStores.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier ];
if([self.checkedIndexPath isEqual:indexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.text= [arrayOfStores objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:#"Buildings2020.png"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath
{
if(self.checkedIndexPath)
{
UITableViewCell* uncheckCell = [tableView
cellForRowAtIndexPath:self.checkedIndexPath];
uncheckCell.accessoryType = UITableViewCellAccessoryNone;
}
if([self.checkedIndexPath isEqual:indexPath])
{
self.checkedIndexPath = nil;
}
else
{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.checkedIndexPath = indexPath;
}
}
In SelectStoreViewController I created delegate with method like below:
SelectStoreViewController.h
#class SelectStoreViewController;
#protocol PassStoreNameDelegate <NSObject>
-(void)setStoreName:(SelectStoreViewController*)controller
didFinishEnteringStoreName:(NSString*)enteredStoreName;
#end
#interface SelectStoreViewController :
UITableViewController<UITableViewDataSource,UITableViewDelegate>{
}
#property (retain,nonatomic) NSIndexPath *checkedIndexPath;
#property (strong,nonatomic) NSMutableArray *arrayOfStores ;
#property (nonatomic,weak) id<PassStoreNameDelegate>delegate;
#end
SelectStoreViewController.m
- (void)viewDidLoad{
[super viewDidLoad];
arrayOfStores = [[NSMutableArray alloc]init];
[self fetchStoresFromDatabase];
self.title = #"Select store from list";
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc]initWithTitle:#"Back"
style:UIBarButtonItemStylePlain target:self
action:#selector(didSelectBackBarButtonItem:)];
self.navigationItem.leftBarButtonItem = backButtonItem;
}
-(void)didSelectBackBarButtonItem:(id)sender{
[self.delegate setStoreName:self didFinishEnteringStoreName:#"store name"];
[self dismissViewControllerAnimated:YES completion:nil];
}
After that I implement PassStoreNameDelegate in NewPackageViewController (in my first UITableViewController)
NewPackageViewController.h
interface NewPackageViewController :
UITableViewController<UITextViewDelegate,PassStoreNameDelegate,CreationDateViewDelegate>
{
SelectStoreViewController *selectStoreNameViewController;
NSString *storeName;
}
#property (nonatomic, retain) SelectStoreViewController*selectStoreNameViewController;
#property (retain,nonatomic) NSString* storeName;
#end
NewPackageViewController.m
-(void)setStoreName:(SelectStoreViewController *)controllerdidFinishEnteringStoreName
(NSString *)enteredStoreName{
self.storeName = enteredStoreName;
[self.tableView reloadData];
}
When trying to perform this task I used this link:Passing Data between View Controllers
I would like to create functionality similar to radio button...
Every help will be appriciated
Ragards
First you should create radio button function in .h file. then implement your code in your 1st view controller.
The only thing you want to do is in 2nd view controller .h file import "secondviewController.h"
Then surely you can use that radio button function in secondviewController.m file

Accessory type for Cross Sign in iOS

I have a table view and 2 custom cells inside it. I want to use a (X) symbol i.e. the cross symbol and the tick symbol. I know for tick we have UITableViewCellAccessoryCheckmark but what to do for the (X) symbol. Can we have any custom accessory for this or else how to achieve this?
My code:
#pragma mark - Table View Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
optionsLabelArray = [[NSArray alloc] initWithObjects:#"Yes", #"No", nil];
static NSString *CellIdentifier = #"CheckerCell";
CustomCell *cell =[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.optionLabel.text = [optionsLabelArray objectAtIndex:indexPath.row];
cell.optionLabelSubtitle.text = [optionsLabelSubtitleArray objectAtIndex:indexPath.row];
if(indexPath.row == 0)
{
cell.textLabel.textColor = [UIColor colorWithRed:78/255.0 green:157/255.0 blue:19/255.0 alpha:1.0];
}
else if(indexPath.row == 1)
{
cell.textLabel.textColor = [UIColor colorWithRed:167/255.0 green:19/255.0 blue:43/255.0 alpha:1.0];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView ==_optionsTableView1) {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
previousSelectedCell1.accessoryType = UITableViewCellAccessoryNone;
cell.accessoryType=UITableViewCellAccessoryCheckmark;
previousSelectedCell1 = cell;
NSLog(#"Row : %ld", (long)indexPath.row);
if(indexPath.row == 0)
{
self.weatherSafeToPlay = YES;
}
else
{
// MatchDayDataController *sharedController = [MatchDayDataController sharedDataController];
// sharedController.actions = [sharedController.actions stringByAppendingString:#"Check Weather. "];
//[sharedController.actions appendString:#"Check Weather. "];
self.weatherSafeToPlay = NO;
}
NSLog(#"Is Weather safe: %hhd", self.weatherSafeToPlay);
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
not heard of any accessory type for cross but in such cases, i simply use unicode characters.
It's lighter than using an image and is a simple matter of just changing the text.
#"\u2713" for ✓
#"\u2717" for ✗
You can:
Create a custom UITableViewCell having a 32by32 (or any dimension) UILabel (on the right/left/middle/wherever)
in -didSelectRowAtIndexPath, you can change the text of this UILabel
#"\u2713" for checkmark or #"\u2717" for crossmark
A simple example (on the default UITableViewCell's default textLabel, just for the idea):
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if([cell.textLabel.text isEqualToString:#"\u2713"]) {
cell.textLabel.text = #"\u2717";
}
else {
cell.textLabel.text = #"\u2713";
}
}
OR... when not using a custom cell, something like:
-(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];
}
//set your text to cell.textLabel.text
//create your own UILabel
UILabel *lblAcc = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 32, 32)];
[lblAcc setTag:100];
[lblAcc setText:#"?"];
[lblAcc setBackgroundColor:[UIColor redColor]];
[lblAcc setTextAlignment:NSTextAlignmentCenter];
//set as custom AccessoryView on cell
[cell setAccessoryView:lblAcc];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *strAccessory = [(UILabel *) [cell viewWithTag:100] text];
//the -isEqualToString is a bit heavier than checking a simple bool or int value
//so since you should be remembering the selected cells anyways, you can change
//the if-logic to compare with a bool rather than the string comparison.
//but for now, we'll do:
if([strAccessory isEqualToString:#"\u2713"]) {
[(UILabel *) [cell viewWithTag:100] setText:#"\u2717"];
}
else {
[(UILabel *) [cell viewWithTag:100] setText:#"\u2713"];
}
}
for more unicode characters... this link:
http://en.wikibooks.org/wiki/Unicode/List_of_useful_symbols
You can assign your own image to accessoryView of table cell. Create an image of (X) symbol and use that image as accessoryView.
cell.accessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"Your X Image"]];

Resources