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;
}
Related
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;
}
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'm currently trying to implement a Table View on my iPhone app that grabs an array from one table view/class and using this array to populate a table in another seperate view. Here is the method I use that adds an exercise to an array if it is clicked/tapped:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *str = cell.textLabel.text;
NSUInteger *index = 0;
NSMutableArray *array = [[NSMutableArray alloc] init];
[array insertObject:str atIndex:index];
self.workout = array;
[array release];
}
Once the save button is pressed, this array will be stored in an array of workouts (arrays) that I want to be populated in another view. Am I taking the correct approach this?
Here is my cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger section = [indexPath section];
NSUInteger row = [indexPath row];
NSString *key = [keys objectAtIndex:section];
NSArray *nameSection = [names objectForKey:key];
static NSString *SectionsTableIdentifier = #"SectionsTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SectionsTableIdentifier ];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier: SectionsTableIdentifier ] autorelease];
}
cell.textLabel.text = [nameSection objectAtIndex:row];
return cell;
}
you probably do not want to re-initialize self.workout with a new array each time.
-(void) viewDidLoad
{
self.workout = [[NSMutableArray alloc] init];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *str = cell.textLabel.text;
[self.workout insertObject:str atIndex:0];
}
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)
I having problem with my table. After loading everything. My data is being loaded and string into name and loadscore. However it will not show in the table. Please help.
static const NSInteger kNameLabelTag = 1337;
static const NSInteger kScoreLabelTag = 5555;
- (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];
UILabel *nameLabel = [[UILabel new] autorelease];
[nameLabel setTag:kNameLabelTag];
[cell.contentView addSubview:nameLabel];
UILabel *highscoreLabel = [[UILabel new] autorelease];
[highscoreLabel setTag:kScoreLabelTag];
[cell.contentView addSubview:highscoreLabel];
}
// Configure the cell.
//cell.textLabel.text = [loadhighScore objectAtIndex:indexPath.row];
NSDictionary *score = [self.loadhighScore objectAtIndex:indexPath.row];
NSString *name = [score objectForKey:#"ScoreName"];
NSString *loadscore = [score objectForKey:#"HighScore"];
[(UILabel *)[cell.contentView viewWithTag:kNameLabelTag] setText:name];
[(UILabel *)[cell.contentView viewWithTag:kScoreLabelTag] setText:loadscore];
return cell;
}
Do you have the tableview delegate and datasource connected to your class?
if you are loading in this data after the view has appeared on screen check that you have reloaded the tables data source [yourTableView reloadData];