How to set checkbox image while selecting table view section? - iphone

I'm working on a project and i stuck at a point. My project has a Table View with some data after clicking on the cell table view will expended into sections. Every thing is fine except one thing i.e i want to set a checkbox image (when the user clicked on a cell).
when i tap on the cell it gives the cell title text but i am not able to set the checkbox image (like UITableViewCellAccessoryCheckmark)on particular cell section.
I am posting the images and the codes below: !
MyTableView it contains the Root Exercise Names
This is the Expended View and i want a checkbox(UITableViewCellAccessoryCheckmark) at the right side of the cell.
When i click on tableView didSelectRowAtIndexPath: it works fine
//code
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
if ([indexPath isEqual:self.selectIndex]) {
self.isOpen = NO;
[self didSelectCellRowFirstDo:NO nextDo:NO];
self.selectIndex = nil;
}else
{
if (!self.selectIndex) {
self.selectIndex = indexPath;
[self didSelectCellRowFirstDo:YES nextDo:NO];
}else
{
[self didSelectCellRowFirstDo:NO nextDo:YES];
}
}
}else
{
NSDictionary *dic = [dataList objectAtIndex:indexPath.section];
NSArray *list = [dic objectForKey:#"ExcerciseList"]; //from plist
NSString *item = [list objectAtIndex:indexPath.row-1];
UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];
if (thisCell.accessoryType == UITableViewCellAccessoryNone)
{
thisCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
thisCell.accessoryType = UITableViewCellAccessoryNone;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:item message:nil delegate:nil cancelButtonTitle:#"ok" otherButtonTitles: nil] ;
[alert show];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark- Table View DataSource and Delegates
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.isOpen&&self.selectIndex.section == indexPath.section&&indexPath.row!=0) {
static NSString *CellIdentifier = #"Cell1";
_cell = (Cell1*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!_cell) {
_cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];
//[[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];
}
NSArray *list = [[dataList objectAtIndex:self.selectIndex.section] objectForKey:#"ExcerciseList"];
_cell.titleLabel.text = [list objectAtIndex:indexPath.row-1];
//[_cell changecheckboxWithUp:([self.selectIndex isEqual:indexPath]?YES:NO)];
return _cell;
}
else
{
static NSString *CellIdentifier = #"view_TableView_List";
cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell=[[CustomCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSString *name = [[dataList objectAtIndex:indexPath.section] objectForKey:#"Excercise_Name"];
cell.Lbl_Excercise_Name.text = name;
cell.Lbl_Excercise_Name.font=[UIFont fontWithName:#"Hustlers Rough Demo" size:40.0f];
NSString *ImageName=[[dataList objectAtIndex:indexPath.section] objectForKey:#"Excercise_Image"];
[cell.imageView_Excercise_Image setImage:[UIImage imageNamed:ImageName]];
[cell changeArrowWithUp:([self.selectIndex isEqual:indexPath]?YES:NO)];
return cell;
}
}
My Question is How to show Checkbox at the selected section?
Sorry! if it is not formatted properly(New to stack)!
Thank You!

I was finally able to get it working. Here's what you have to change:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.isOpen&&self.selectIndex.section == indexPath.section&&indexPath.row!=0) {
static NSString *CellIdentifier = #"Cell1";
_cell = (Cell1*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!_cell) {
_cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];
//[[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];
}
NSArray *list = [[dataList objectAtIndex:self.selectIndex.section] objectForKey:#"ExcerciseList"];
_cell.titleLabel.text = [list objectAtIndex:indexPath.row-1];
_cell.accessoryView = nil;
if ([self.selectedCells containsObject:indexPath])
{
_cell.accessoryView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:#"check_box#2x.png"]];
}
//[_cell changecheckboxWithUp:([self.selectIndex isEqual:indexPath]?YES:NO)];
return _cell;
}
else
{
static NSString *CellIdentifier = #"view_TableView_List";
cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell=[[CustomCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSString *name = [[dataList objectAtIndex:indexPath.section] objectForKey:#"Excercise_Name"];
cell.Lbl_Excercise_Name.text = name;
cell.Lbl_Excercise_Name.font=[UIFont fontWithName:#"Hustlers Rough Demo" size:40.0f];
NSString *ImageName=[[dataList objectAtIndex:indexPath.section] objectForKey:#"Excercise_Image"];
[cell.imageView_Excercise_Image setImage:[UIImage imageNamed:ImageName]];
[cell changeArrowWithUp:([self.selectIndex isEqual:indexPath]?YES:NO)];
return cell;
}
}
And
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
if ([indexPath isEqual:self.selectIndex]) {
self.isOpen = NO;
[self didSelectCellRowFirstDo:NO nextDo:NO];
self.selectIndex = nil;
}else
{
if (!self.selectIndex) {
self.selectIndex = indexPath;
[self didSelectCellRowFirstDo:YES nextDo:NO];
}else
{
[self didSelectCellRowFirstDo:NO nextDo:YES];
}
}
}else
{
NSDictionary *dic = [dataList objectAtIndex:indexPath.section];
NSArray *list = [dic objectForKey:#"ExcerciseList"];
NSString *item = [list objectAtIndex:indexPath.row-1];
UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];
if (!thisCell.accessoryView)
{
[self.selectedCells addObject:indexPath];
thisCell.accessoryView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:#"check_box#2x.png"]];
}
else
{
[self.selectedCells removeObject:indexPath];
thisCell.accessoryView = nil;
}
// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:item message:nil delegate:nil cancelButtonTitle:#"ok" otherButtonTitles: nil] ;
// [alert show];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
If nothing else has changed in those methods, just copy-paste the code and it will work. I tested it before posting it. Let me know if you have more questions
UPDATE
Here is the explanation of what was going on. There were actually a couple of problems:
First, you were mixing accessoryType and accessoryView; this was causing an
incorrect execution of the if-else clause. Here is the old code
// here you are using accessoryType
if (thisCell.accessoryType == UITableViewCellAccessoryNone)
{
// but here you're using accessoryView
thisCell.accessoryView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:#"check_box#2x.png"]];
}
else
{
// here too...
thisCell.accessoryView=nil;
thisCell.accessoryView = nil;
}
Second, you were not saving the indexPaths of the selected elements. Here is the corrected code (note is the same if-else clause):
if (!thisCell.accessoryView)
{
// you have to save the selected indexPaths
[self.selectedCells addObject:indexPath];
thisCell.accessoryView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:#"check_box#2x.png"]];
}
else
{
// if the cell was already selected, then remove the indexPath
[self.selectedCells removeObject:indexPath];
thisCell.accessoryView = nil;
}
Third, the following line inside of cellForRowAtIndexPath... was messing up things because it was showing another checkmark, so I just removed it:
[_cell.checkUnCheck_ImageView setHidden:NO];
That was pretty much it.
Hope this helps!

You have the right idea of how to do this, however, you're setting the checkmark in the wrong place. You should use the method -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath to store (remember) which cell was selected and NOT to set the accessory type.
If only one cell can be selected at a time you can declare aNSIndexPath property to remember the cell that was selected. However, if multiple cells can be selected at a time you can declare a NSMutableArray where you can store all the cells that have been selected.
Then you should use the method -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath to set the checkmark to the cells that were selected.
Here is an example (I'm assuming multiple cells can be selected):
In your *.h file declare a NSMutableArray property
#property (nonatomic, strong) NSMutableArray *selectedCells;
In your *.m file, inside your init method initialize the property:
_selectedCells = [[NSMutableArray alloc] init];
Then, in the didSelectRowAtIndexPath... method:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([self.selectedCells containsObject:indexPath]) {
[self.selectedCells removeObject:indexPath]; // this line allows the checkmark to be shown/hidden if the user presses several times the same cell
} else {
[self.selectedCells addObject:indexPath]; // a cell was selected; remember it
}
... // the rest of your code
}
Finally, in the cellForRowAtIndexPath... method
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
... // Create, initialize and customize your cell
// if the cell you're being asked for is saved in the array it means it was selected
if ([self.selectedCells containsObject:indexPath]) {
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
}
And that's it. Let me know if you have more questions
Hope it helps!
UPDATE
If all you want to do is to set a custom image as your checkmark just create the image and assign it to the accessory view while using the code that was already working for you, like so:
// Note that you should use 'accessoryView' and NOT 'accessoryType'
if (thisCell.accessoryView == UITableViewCellAccessoryNone)
{
thisCell.accessoryView = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:#"YOUR_IMAGE_NAME"]];
}
else
{
thisCell.accessoryView = nil;
}

Related

UITableView Crashes when scrolling and [reloadData]

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];

UITableView use plist make a pervious and next button?

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.

CheckBox Item in Custom Designed UITableViewCell , IPhone SDK

I have designed a custom table cell. which displays product information.
When i implement CellForRowAtIndexPath I am doing this.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *sectionTableCellIdentifier = [[NSString alloc] initWithFormat:#"GLItemTableCellIdentifierNumber%d",indexPath.section];
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"GLItemDetailsTableCellIdentifier"];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:sectionTableCellIdentifier];
if (cell == nil)
{
NSDictionary *dict = [self.listData objectAtIndex:indexPath.row];
ItemsListTableCell *cell = (ItemsListTableCell *)[tableView dequeueReusableCellWithIdentifier:sectionTableCellIdentifier];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ItemsListTableCell"
owner:self options:nil];
for (id oneObject in nib)
{
if ([oneObject isKindOfClass:[ItemsListTableCell class]])
{
cell = (ItemsListTableCell *)oneObject;
}
}
NSString *priceinfo = [[NSString alloc] initWithFormat:#"$%#",[dict objectForKey:#"CurrentPrice"]];
NSString *sizeinfo = [[NSString alloc] initWithFormat:#"Size: %#",[dict objectForKey:#"Size"]];
NSString *upcInfo = [[NSString alloc] initWithFormat:#"UPC: %#",[dict objectForKey:#"ID"]];
NSString *strQuantity = [[NSString alloc] initWithFormat:#"%#",[dict objectForKey:#"Quantity"]];
cell.lblProductName.text = [dict objectForKey:#"Name"];
cell.lblSize.text = sizeinfo;
cell.lblBrand.text = [dict objectForKey:#"BrandName"];
cell.lblProductCode.text = upcInfo;
cell.lblQuantity.text = strQuantity;
cell.lblPrice.text = priceinfo;
cell.lblStoreName.text = [dict objectForKey:#"StoreName"];
cell.isSelected = NO;
[cell.btnSelected addTarget:self action:#selector(cellButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];
[upcInfo release];
[priceinfo release];
[strQuantity release];
[sizeinfo release];
return cell;
}
return cell;
}
now for the click event I am doing
- (IBAction)cellButtonTapped:(id)sender
{
UIView *contentView = [sender superview];
ItemsListTableCell *cell = (ItemsListTableCell *)[contentView superview];
NSIndexPath *indexPath = [table indexPathForCell:cell];
NSUInteger buttonRow = [[self.table
indexPathForCell:cell] row];
NSUInteger buttonSection = [[self.table
indexPathForCell:cell] section];
NSLog(#"Index Path Row : %d",buttonRow);
NSLog(#"Index Path Section : %d",buttonSection);
ItemsListTableCell *buttonCell =
(ItemsListTableCell *)[table cellForRowAtIndexPath:indexPath];
if (buttonCell.isSelected == YES)
{
buttonCell.isSelected = NO;
UIImage *image = [[UIImage imageNamed:#"checkbox-empty.png"] autorelease];
[buttonCell.btnSelected setImage:image forState:UIControlStateNormal];
}
else
{
buttonCell.isSelected = YES;
UIImage *image = [[UIImage imageNamed:#"checkbox-full.png"] autorelease];
[buttonCell.btnSelected setImage:image forState:UIControlStateNormal];
}
self.txtQuantity.text = buttonCell.lblQuantity.text;
NSString *buttonTitle = buttonCell.lblProductName.text;
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"You tapped the button"
message:[NSString stringWithFormat:
#"You tapped the button for %#", buttonTitle]
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
The Problem is when i click on check button it is going to the event. but I am unable to detect what is the parent cell . as there are some values in cell.
Instead of creating such an event (IBAction), you could do all of these in
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if (selectedCell.accessoryType == UITableViewCellAccessoryNone)
{
selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
if (selectedCell.accessoryType == UITableViewCellAccessoryCheckmark)
{
selectedCell.accessoryType = UITableViewCellAccessoryNone;
}
}
If you want a checkmark of your own style, you could set them up here and finish off things. Makes stuff easier !

Add images to table cells in sections

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;
}

Load Distinct Images from array into UITabeView

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)