I am trying to load three different images from an array into corresponding cells in a UITable. So far I have the following code which builds fine crashes when t is run. I fanyone can helps me out I would be very grateful.
- (void)viewDidLoad {
NSArray *array = [[NSArray alloc] initWithObjects:#"Icon Nightclub", #"Smyhts Bar",
#"Synotts",nil];
self.listData = array;
NSArray *picArray = [[NSArray alloc] initWithObjects:#"ArrowLeftDefault.png", #"ArrowRightDefault.png",
#"events.png",nil];
self.picData = picArray;
[array release];
[picArray release];
[super viewDidLoad];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [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.textColor = [UIColor grayColor];
cell.textLabel.text = [listData objectAtIndex:row];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
cell.imageView.image = [picData objectAtIndex:indexPath.row];
return cell;
}
change:
cell.imageView.image = [picData objectAtIndex:indexPath.row];
for something like this:
cell.imageView.image = [UIImage imageNamed:[picData objectAtIndex:indexPath.row]];
Explanation:
Since picData NSArray contains NSStrings, you were passing a NSString object to cell.imageView.image when it expects an UIImage object actually.
That is why now it creates an image and passes it. (imageNamed: method cashes the image)
Related
I'm creating a app using theos (jailbreak) and I have created a UITableView in the loadView method in my RootViewController.mm file:
- (void)loadView {
self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];
self.view.backgroundColor = [UIColor redColor];
NSError * error;
NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:#"/var/mobile/Library/BannerImage" error:&error];
countries = [NSDictionary dictionaryWithObject:directoryContents forKey:#"Themes"];
mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStyleGrouped];
[mainTableView setDataSource:self];
[mainTableView setDelegate:self];
[self.view addSubview:mainTableView];
}
And the rest of my code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [countries count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [[countries allKeys] objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString *continent = [self tableView:tableView titleForHeaderInSection:section];
return [[countries valueForKey:continent] 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];
}
// Configure the cell...
NSString *continent = [self tableView:tableView titleForHeaderInSection:indexPath.section];
NSString *country = [[countries valueForKey:continent] objectAtIndex:indexPath.row];
cell.textLabel.text = country;
cell.accessoryType = UITableViewCellAccessoryNone;
if([self.checkedIndexPath isEqual:indexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *object = [tableView cellForRowAtIndexPath:indexPath];
[object setSelected:NO animated:YES];
NSString *selectedTheme = [[object textLabel] text];
NSDictionary *plistFile = [NSDictionary dictionaryWithObject:selectedTheme forKey:#"CurrentTheme"];
[plistFile writeToFile:#"/Applications/BannerImage.app/CurrentTheme.plist" atomically:YES];
if(self.checkedIndexPath)
{
UITableViewCell* uncheckCell = [tableView
cellForRowAtIndexPath:checkedIndexPath];
uncheckCell.accessoryType = UITableViewCellAccessoryNone;
}
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.checkedIndexPath = indexPath;
}
It crashes when I scroll up and try to scroll back down and it crashes when [mainTableView reloadData] is called. How can I fix this?
Crash Log
I found out that I needed to retain the Dictionary because it was being released. So instead of
NSDictionary *countries;
change to this
#property(nonatomic, retain) NSDictionary *countries;
and
#synthesize countries;
in your .mm file and use
self.countries = [NSDictionary dictionaryWithObject:directoryContents forKey:#"Themes"];
instead of
countries = [NSDictionary dictionaryWithObject:directoryContents forKey:#"Themes"];
Hi, Please check there if is any data in countries and "continent" value key is presented or not in dic before this line
NSString *country = [[countries valueForKey:continent] objectAtIndex:indexPath.row];
I use UITableView with plist to make a tableview, than how to make a pervious/next button within the DetailView (without back to tableview and press next item)?
The program:
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableDictionary *labelDict = [[[NSMutableDictionary alloc]initWithContentsOfFile:[[[NSBundle mainBundle]bundlePath]stringByAppendingPathComponent:#"myData.plist"]] retain];
cellImages = [[NSMutableArray alloc ] initWithArray:[labelDict objectForKey:#"image"]];
cellTitles = [[NSMutableArray alloc] initWithArray:[labelDict objectForKey:#"title"]];
cellSubTitles = [[NSMutableArray alloc] initWithArray:[labelDict objectForKey:#"subtitle"]];
NSLog(#"%#", [[[NSBundle mainBundle] bundlePath]stringByAppendingPathComponent:#"myData.plist"]);
}
- (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];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
// Configure the cell.
cell.textLabel.text = [cellTitles objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [cellSubTitles objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:[cellImages objectAtIndex:indexPath.row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!self.detailViewController) {
self.detailViewController = [[[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil] autorelease];
}
[self.navigationController pushViewController:self.detailViewController animated:YES];
self.detailViewController.detailImage.image = [UIImage imageNamed:[cellImages objectAtIndex:indexPath.row]];
self.detailViewController.detailDescriptionLabel.text = [cellTitles objectAtIndex:indexPath.row];
self.detailViewController.subTitle.text = [cellSubTitles objectAtIndex:indexPath.row];
self.detailViewController.title = nil;
}
Pass data arrays and current index to the detail view controller. Add actions to the buttons that will be selecting current record to dislpay.
I have an issue of alphabetically sorting the contacts picked from the address book into section headers . I can arrange them alphabetically without the section headers but how to put them according to the names of the contacts ? I do not want to use the NSDictionary as I am able to do the sorting without it. Please view the code below :-
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [arrayForLastName count];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [[alphabets sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)] objectAtIndex:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return [NSArray arrayWithObjects:#"A",#"B",#"C",#"D",#"E",#"F",#"G",#"H",#"I",#"J",#"K",#"L",#"M",#"N",#"O",#"P",#"Q",#"R",#"S",#"T",#"U",#"V",#"W",#"X",#"Y",#"Z", nil];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [arrayForLastName count];
}
Any help would be appreciated thanx :)
You can achieve this in following way -
1.Create an index array that contains all the required index -
NSArray *indexArray = [NSArray arrayWithObjects:#"A",#"B",#"C",#"D",#"E",#"F",#"G",#"H",#"I",#"J",#"K",#"L",#"M",#"N",#"O",#"P",#"Q",#"R",#"S",#"T",#"U",#"V",#"W",#"X",#"Y",#"Z", nil];
2.For Each index you need to have an array that will display row for those sections. SO you can have a dictionary that contain an array corresponding to each index.
Check this tutorial, it will help you in implementing this- http://www.icodeblog.com/2010/12/10/implementing-uitableview-sections-from-an-nsarray-of-nsdictionary-objects/
Try this code in table delegate methode.
1) - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [dataArray1 count];
}
return number of section in your table view.
2) -(NSMutableArray *)selectedarray:(NSString *)countrynmae
{
NSString *strQuery=[NSString stringWithFormat:#" select * from Globle where Country='%#'",countrynmae];
NSMutableArray *dataarray=[Database executeQuery:strQuery];
return dataarray;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSDictionary *dict =[dataArray1 objectAtIndex:section];
NSString *strcountryname=[dict objectForKey:#"Country"];
NSMutableArray *arr6 = [self selectedarray:strcountryname ];
return [arr6 count];
}
Return number of row in particular section
3) - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// create the parent view that will hold header Label
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 300.0, 44.0)];
// create the button object
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
headerLabel.textColor = [UIColor whiteColor];
headerLabel.font = [UIFont boldSystemFontOfSize:16];
headerLabel.frame = CGRectMake(20.0, -14.0, 300.0, 30.0);
NSDictionary *dict =[dataArray1 objectAtIndex:section];
headerLabel.text=[dict objectForKey:#"Country"];
headerLabel.textColor=[UIColor redColor];
[customView addSubview:headerLabel];
return customView;
}
set header name for section
4) - (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];
cell.textLabel.textColor=[UIColor whiteColor];
cell.backgroundColor=[UIColor clearColor];
}
NSDictionary *dict =[dataArray1 objectAtIndex:indexPath.section];
NSString *countryname = [dict objectForKey:#"Country"];
NSString *strQuery = [NSString stringWithFormat:#"Select * from Globle where Country = '%#'",countryname];
NSMutableArray *arr = [Database executeQuery:strQuery];
NSDictionary *dict1 = [arr objectAtIndex:indexPath.row];
cell.textLabel.text=[dict1 objectForKey:#"Name"];
cell.textLabel.numberOfLines=1;
cell.textLabel.textColor=[UIColor blackColor];
cell.backgroundColor=[UIColor clearColor];
cell.textLabel.textAlignment=UITextAlignmentCenter;
NSString *strImage=[dict1 objectForKey:#"ImageName"];
UIImageView *imageView1 = [[UIImageView alloc] init];
UIImage *image1 = [[UIImage alloc] initWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:strImage ofType:#"jpg"]];
imageView1.image = image1;
imageView1.frame = CGRectMake(6,2,50,42); // position it to the middle
[cell.contentView addSubview:imageView1];
return cell;
[tbl reloadData];
}
return cell in uitable view
i want to display an image besides the text in table cell. i have 2 sections, General and Assignments. Now i only want to display image beside Assignments and no image besides General section. the below code displays images for Assignments section and then again repeats the images for General section. Can anybody help me how to accompalish this.
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
staffImages = [[NSMutableArray alloc] init];
NSArray *arrTemp1 = [[NSArray alloc] initWithObjects:#"TRANSPORTATION",#"ROOMS",#"ACTIVITIES",nil];
NSArray *arrTemp2 = [[NSArray alloc] initWithObjects:#"CHAT",#"SEARCH",#"CALL",#"MORE",nil];
NSDictionary *temp =[[NSDictionary alloc] initWithObjectsAndKeys:arrTemp1,#"Assignments",arrTemp2,
#"General",nil];
//Add item images
[staffImages addObject:#"transportation.png"];
[staffImages addObject:#"hotel.png"];
[staffImages addObject:#"activities.png"];
//Set the title
self.navigationItem.title = #"STAFF ASSIGNMENTS";
self.tableContents=temp;
[temp release];
self.sortedKeys =[self.tableContents allKeys];
[arrTemp1 release];
[arrTemp2 release];
[super viewDidLoad];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]];
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];
cell.imageView.image = [UIImage imageNamed:[staffImages objectAtIndex:indexPath.row]];
return cell;
}
NSMutableData *data=[NSMutableData dataWithContentsOfURL:[NSURL URLWithString:[NSMutableString stringWithFormat:#"%#",[[dataArray objectAtIndex:indexPath.row]objectForKey:#"LINK"]]]];
//NSLog(#"%#",data);
UIImage *img=[UIImage imageWithData:data];
cell.imageView.image=img;
this is best way
Assuming the general section is section 0 (i.e. first section)
if (indexPath.section == 1) {
cell.imageView.image = [UIImage imageNamed:[staffImages objectAtIndex:indexPath.row]];
}
You need to check the section of the indexPath. You probably will also need to use the section check to make sure that you set the text of the cell to the right value.
//You do not need to actually define this value
//this is just for naming purposes
#define SECTION_ASSIGNMENTS 0
...
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]];
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];
if(indexPath.section == SECTION_ASSIGNMENTS)
{
cell.imageView.image = [UIImage imageNamed:[staffImages objectAtIndex:indexPath.row]];
}
return cell;
}
HI,
how to implement checkmark, one at a time in a uitableview.and then how to save that state in my application? please guide.
right now i am having a table and the data in table row is coming from a nsmutablearray.My .m file is as:
#import "LocationSelection.h"
#implementation LocationSelection
#synthesize menuList, table;
- (void)viewDidLoad {
menuList=[[NSMutableArray alloc] initWithObjects:
[NSArray arrayWithObjects:#"LOCATION1",nil],
[NSArray arrayWithObjects:#"LOCATION2",nil],
[NSArray arrayWithObjects:#"LOCATION3",nil],
[NSArray arrayWithObjects:#"LOCATION4",nil],
[NSArray arrayWithObjects:#"LOCATION5",nil],
[NSArray arrayWithObjects:#"LOCATION6",nil],
[NSArray arrayWithObjects:#"LOCATION7",nil],
nil];
[self.navigationController setNavigationBarHidden:NO];
self.navigationController.navigationBar.tintColor=[UIColor blackColor];
self.navigationController.navigationBar.barStyle=UIBarStyleBlackTranslucent;
self.title=#"Location Selection";
[table reloadData];
[super viewDidLoad];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView{
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 40;
}
- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section{
return menuList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero] autorelease];
}
cell.highlighted=NO;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
NSArray *rowArray = [menuList objectAtIndex:indexPath.row];
UILabel *nameLabel = [[[UILabel alloc] initWithFrame:CGRectMake(15, 8, 200, 20)]autorelease];
nameLabel.text = [NSString stringWithFormat:#"%#",[rowArray objectAtIndex:0]];
nameLabel.backgroundColor = [UIColor clearColor];
nameLabel.shadowColor=[UIColor whiteColor];
nameLabel.shadowOffset=CGSizeMake(0.0, 0.5);
nameLabel.textColor = RGB(0,0,0);
[nameLabel setFont:[UIFont boldSystemFontOfSize:16.0f]];
[cell.contentView addSubview:nameLabel];
return cell;
}
Thanks!
Comment this line in cellForRowAtIndexPath -> cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
Use this:
NSIndexPath* lastIndexPath; // This as an ivar
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* newCell = [tableView cellForRowAtIndexPath:indexPath];
int newRow = [indexPath row];
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
if(newRow != oldRow)
{
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell* oldCell = [tableView cellForRowAtIndexPath:lastIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
lastIndexPath = indexPath;
}
}
In your MenuList you should stock NSDictionaries having 2 keys:
location
visited (if the location should have the checkmark)
NSDictionary *loc = [[NSDictionary alloc] initWithObjectsAndKeys
#"Location 1", #"location",
#"NO", #"visited", nil, nil];
When setting up the cell you would test to see if the "visited" key has a value:
if ([[menuList objectAtIndex: indexPath.row] valueForKey:#"visited"]){
// the location was visited
// setup the checkmark
}
Lastly, to fill in the name of the location, instead of:
nameLabel.text = [NSString stringWithFormat:#"%#",[rowArray objectAtIndex:0]];
put
nameLabel.text = [[rowArray objectAtIndex:0] valueForKey:#"location"];
Hope this helps
Use
cell.accessoryType = UITableViewCellAccessoryCheckmark;
Instead of
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
in your code ...