How to pass data between two UITableViewControllers - ios

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

Related

How to add images in UITableViewCells programmatically in iOS

I need to add different images in UITableViewCells programmatically. How can I do that. I am trying some code. But the images are not displayed in UITableViewCells.
This is my code below.
-(void)viewDidLoad
{
arrImages=[[NSMutableArray alloc]initWithObjects:#"image.png",#"image2.png",#"image3.png",#"image4.png",#"image5.png",#"image6.png",#"image7.png",#"image8.png" nil];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier=#"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"identifier"];
}
imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 48, 48)];
cell.imageView.image = [UIImage imageNamed:[arrImages objectAtIndex:indexPath.row]];
return cell;
}
This is what you probably want:
- (void)viewDidLoad
{
self.arrImages = [[NSMutableArray alloc] initWithObjects:#"image.png",#"image2.png",#"image3.png",#"image4.png",#"image5.png",#"image6.png",#"image7.png",#"image8.png" nil];
}
- (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]
}
cell.imageView.image = [UIImage imageNamed:[self.arrImages objectAtIndex:indexPath.row]];
return cell;
}
P.S.: Next time, please make some effort on formatting the source code, explaining what is wrong, what have you tried, what would you like to accomplish. Thanks
Create a property of NSArray in your .h file
#property NSArray *imgArray;
synthesize in your .m file
#synthesize imgArray;
write this in your viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
// Initialize images...
imgArray = [NSArray arrayWithObjects:#"yourFirstImageName.png", #"yourSecondImageName.png", #"yourThirdImageName.png",....., #"yourSeventhImageName.png", nil];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 48, 48)]; // your cell's height should be greater than 48 for this.
imgView.tag = 1;
[cell.contentView addSubview:imgView];
imgView = nil;
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
lbl.backgroundColor = [UIColor clearColor];
[lbl setTag:2];
[cell.contentView addSubview:lbl];
lblDisch = nil;
}
UIImageView *_imgView = (UIImageView *)[cell.contentView viewWithTag:1];
_imgView.image = [UIImage imageNamed:[imgArray objectAtIndex:indexPath.row]];
UILabel *_lbl = (UILabel *)[cell.contentView viewWithTag:2];
_lbl.text = [arrFont objectAtIndex:indexPath.row]; // arrFont is your array
return cell;
}
Hope this will help you.
Create a #property of NSMutableArray in your .h file.
NSMutableArray *data;
Write This Code your .m file.
- (void)viewDidLoad
{
data = [[NSMutableArray alloc]initWithObjects:#"1.jpg",#"2.jpg",#"3.jpg",#"4.jpg",#"5.jpg",#"6.jpg",#"7.jpg",#"8.jpg",#"9.jpg",#"10.jpg",#"11.jpg",#"12.jpg",#"13.jpg",nil];
[super viewDidLoad];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [data count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.imageView.image = [UIImage imageNamed:[data objectAtIndex:indexPath.row]];
return cell;
}

how can i display UITextfields inside of UITableviewcells if user click the "+" Button in UIView

I have one "+" button in UIView. My requirement is, if i click that button UITextFields displayed in UITableViewCells. I am having idea how to display the UITextFields in UIView if user clicks the "+" button. But i dont have any idea how to display UITextFields inside of UITableViewCells if user hits the "+" button. Please kindly help me anybody. Yesterday i strucked with this functionality. I tried some code. But it is not worked properly. But i had not found any solution. Thanks in advance.
UIButton *myGreenIconButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
[myGreenIconButton1 addTarget:self action:#selector(GreenIconButtonClicked)forControlEvents:UIControlEventTouchUpInside];
[myGreenIconButton1 setBackgroundImage:[UIImage imageNamed:#"index.jpg"] forState:UIControlStateNormal];
myGreenIconButton1.backgroundColor = [UIColor clearColor];
myGreenIconButton1.frame = CGRectMake(285, 144, 25, 25);
[self.view addSubview:myGreenIconButton1];
-(void)GreenIconButtonClicked
{
view=[[UIView alloc]initWithFrame:CGRectMake(0, 80, 300, 20)];
text1=[[UITextField alloc]initWithFrame:CGRectMake(10, 80, 100, 20)];
text1.borderStyle=UITextBorderStyleRoundedRect;
text1.backgroundColor=[UIColor clearColor];
text1.backgroundColor=[UIColor colorWithRed:0.662745 green:0.662745 blue:0.662745 alpha:0.5];
text1.font=[UIFont systemFontOfSize:14.0];
text1.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
text1.textAlignment=NSTextAlignmentCenter;
text1.delegate=self;
[view addSubview:text1];
text2=[[UITextField alloc]initWithFrame:CGRectMake(120, 80, 100, 20)];
text2.borderStyle=UITextBorderStyleRoundedRect;
text2.backgroundColor=[UIColor clearColor];
text2.backgroundColor=[UIColor colorWithRed:0.662745 green:0.662745 blue:0.662745 alpha:0.5];
text2.font=[UIFont systemFontOfSize:14.0];
text2.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
text2.textAlignment=NSTextAlignmentCenter;
text2.delegate=self;
[view addSubview:text2];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
[cell.textLabel setText:#""];
[view setTag:101];
NSIndexPath *ip = [NSIndexPath indexPathForRow:0 inSection:0];
UITableViewCell *cell1 = [self.tableView cellForRowAtIndexPath:ip];
UITextField *textField = (UITextField*)[cell1 viewWithTag:101];
UITextField *textField1=(UITextField*)[cell1 viewWithTag:101];
UITextField *textField2=(UITextField*)[cell1 viewWithTag:101];
[cell.contentView addSubview:text1];
[cell.contentView addSubview:text2];
[cell.contentView addSubview:text3];
return cell;
}
I have created a example for your requirement and posted it on github. You can find code Here
I have created a custom table view cell with text field on it.
First create custom cell with xib or without and do following manner its working for you :
CustomCell.h
#import <UIKit/UIKit.h>
#interface CustomCell : UITableViewCell {
}
#property (strong,nonatomic) IBOutlet UITextField *textField;
#end
CustomCell.m
#import "CustomCell.h"
#implementation CustomCell
#synthesize textField;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.textField = [[UITextField alloc] init];
[self.contentView addSubview:self.textField];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
Your TableView .h
#property (strong,nonatomic) NSMutableArray *array;
#property (strong, nonatomic) IBOutlet UITableView *tblView;
Your Table View .m
#synthesize array;
#synthesize tblView;
-(void)viewWillAppear:(BOOL)animated {
array = [[NSMutableArray alloc] init];
[tblView reloadData];
}
-(int)numberOfSectionsInTableView:(UITableView *)tableView
{
if( [array count ] > 0 )
{
return 1;
}
return 0;
}
-(int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if( [array count ] > 0 )
{
return [array Count];
}
return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = [NSString stringWithFormat:#"CustomCell%d%d",indexPath.row,indexPath.section];
[tblCreateProfile registerNib:[UINib nibWithNibName:#"CustomCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if(cell == nil)
{
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cellIdentifier =nil;
cell.textField.text = [array objectAtIndex:indexPath.row];
return cell;
}
-(void)GreenIconButtonClicked
{
[array addObject:#"Test"];
[tblView reloadData];
}
So just try in Custom TableViewCells. So that You can Achieve the solution for this problem.

Add subview to UITableViewCell as replacement for cell selection

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

ReloadData (UITableView) doesn't work

I got a big problem.
[self.tableView reloadData];
Doesn't work, and I don't understand why.
[[self tableView] reloadData];
Doesn't work too.
Here is my code:
.h
#interface ArticleViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate>
{
}
#end
.m
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.dataSource = self;
self.tableView.delegate = self;
}
btnLeft = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"btnLeft"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(loadPlist)];
self.navigationItem.leftBarButtonItem = btnLeft;
In the loadPlist method, I'm writing in a .plist file. This part work fine.
Once all is write in the .plist file :
btnRight = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"btnRight"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(reloadTheTB)];
self.navigationItem.rightBarButtonItem = btnRight;
- (void)reloadTheTB {
NSLog(#"Test reloadTheTB");
[[self tableView] reloadData];
}
If I touch btnRight, I can see in the log "Test reloadTheTB".
Here is tableView:cellForRowAtIndexPath:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
cell = [self getCellContentView:CellIdentifier];
contentDictio = [dict objectAtIndex:indexPath.row];
UILabel *lblTemp1 = (UILabel *)[cell viewWithTag:1];
lblTemp1.text = [contentDictio objectForKey:#"title"];
if(indexPath.row % 2) {
UIView* myBackgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
myBackgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"cell_grise"]];
cell.backgroundView = myBackgroundView;
}
else {
UIView* myBackgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
myBackgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"cell_blanche"]];
cell.backgroundView = myBackgroundView;
}
}
return cell;
}
UPDATE:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [dict count];
}
Help me please...
Actually I'm wondering this how you know that reloadData is not called?
Try to put some log in cellForRowAtIndexPath? to see if is called or not when you click your button? and also in do this :
- (void)reloadTheTB {
NSLog(#"Test reloadTheTB");
NSLog(#"%#",dict);
[[self tableView] reloadData];
}
Is the content of your dictionary what you expect? or is not changed after loadPlist?
Probably the call to reloadData is not executed on the main thread and you can update the UI only on the main thread. Try to execute your method on the main thread doing something like this :
-(void)ReloadTheTB{
[self.tableView performSelectorOnMainThread#selector(reloadData) withObject:nil waitUntilDone:NO]
}

How to add images to UITableView cell?

I temporarly added some values to the UITableViewCell programmatically. But I need to add images to each cell. How can I do that?
Here is my code. .h file
#interface BidalertsViewController : UIViewController <UITableViewDelegate,UITableViewDataSource> {
NSArray *listData;
}
#property(nonatomic, retain) NSArray *listData;
#end
.m file
#synthesize listData;
- (void)viewDidLoad {
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(5,0,310,28)];
newView.backgroundColor=[UIColor whiteColor];
UITextView *mytext = [[UITextView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 25.0)];
mytext.backgroundColor = [UIColor clearColor];
mytext.textColor = [UIColor blackColor];
mytext.editable = NO;
mytext.font = [UIFont systemFontOfSize:15];
mytext.text = #"Asset Name";
[mytext release];
[newView addSubview:mytext];
[self.view addSubview:newView];
[newView release];
NSArray *array = [[NSArray alloc] initWithObjects:#"iPhone", #"iPod", #"iPad",nil];
self.listData = array;
[array release];
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)dealloc {
[listData dealloc];
[super dealloc];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.listData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if (cell == nil) { cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
return cell;
UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 20, 25)];
imv.image=[UIImage imageNamed:#"user.jpg"];
[cell.contentView addSubview:imv];
[imv release];
}
#end
The code didn't add images to the cell. What's the problem? How can I do to add custom image to cell?
Move return cell; at the end of the method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if (cell == nil) { cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 20, 25)];
imv.image=[UIImage imageNamed:#"user.jpg"];
[cell.contentView addSubview:imv];
[imv release];
return cell;
}
You can add images using the imageView property of a cell:
cell.imageView.image = [UIImage imageNamed:#"user.jpg"];
in your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method.
Here's an example cell loading a facebook image in a table cell.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
// Leave cells empty if there's no data yet
if (nodeCount > 0)
{
// Set up the cell...
AppRecord *appRecord = [self.friendsList objectAtIndex:indexPath.row];
cell.textLabel.text = friend.name;
// Only load cached images; defer new downloads until scrolling ends
if (!appRecord.icon)
{
// set the url
appRecord.imageURLString = [NSString stringWithFormat:#"http://graph.facebook.com/%#/picture", friend.recordID];
// request the download
if (self.tableView.dragging == NO && self.tableView.decelerating == NO)
{
[self startIconDownload:appRecord forIndexPath:indexPath];
}
// if a download is deferred or in progress, return a placeholder image
cell.imageView.image = [UIImage imageNamed:#"Placeholder.png"];
}
else
{
cell.imageView.image = appRecord.icon;
}
}
return cell;
you adding it right, but you fogot one thing:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if (cell == nil) { cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row]; cell.textLabel.text = [listData objectAtIndex:row];
UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 20, 25)];
imv.image=[UIImage imageNamed:#"user.jpg"];
[cell.contentView addSubview:imv];
[imv release];
//you fogot this:
return cell;
}
// create a rectangle box for image view
UIImageView *iconImage = [[UIImageView alloc] init];
iconImage.frame = CGRectMake(12,4,53.5,53.5);
// Create a label for cells
UILabel *cellName = [[UILabel alloc] initWithFrame:CGRectMake(75,18,200,20)];
cellName.font = [self fontForBodyTextStyle];
NSString *cellNameStr = [NSString stringWithFormat:#"%#",self.tableCellNames[indexPath.row]];
// Select path row tab actions
switch (indexPath.row) {
case 0:
cellName.text = cellNameStr;
iconImage.image = [UIImage imageNamed:#"condition.png"];
[cell.contentView addSubview:iconImage];
[cell.contentView addSubview:cellName];
break;
case 1:
cellName.text = cellNameStr;
iconImage.image = [UIImage imageNamed:#"videos.png"];
[cell.contentView addSubview:iconImage];
[cell.contentView addSubview:cellName];
break;
case 2:
cellName.text = cellNameStr;
iconImage.image = [UIImage imageNamed:#"provider.png"];
[cell.contentView addSubview:iconImage];
[cell.contentView addSubview:cellName];
break;
case 3:
cellName.text = cellNameStr;
iconImage.image = [UIImage imageNamed:#"info.png"];
[cell.contentView addSubview:iconImage];
[cell.contentView addSubview:cellName];
break;
default:
NSAssert(NO, #"Unhandled value in cellForRowAtIndexPath");
break;
}
Thank VKJ,

Resources