custom cell not appear when loading new views objective-C - objective-c

I create a UITableView programmatically with different cells and sections that connects to the other views in storyboard
But if you check the story board absence view has custom cell with check box sections that it's not appear here
My question is:
why it doesn't shows the custom cell?,would you please helping me
Thanks in advance!
Here is my code:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString: #"WorkTime"]){
[segue.destinationViewController setTitle:#"WorkTime"];
}if([segue.identifier isEqualToString: #"Absence"]){
[segue.destinationViewController setTitle:#"Absence"];
}if([segue.identifier isEqualToString: #"Compensation"]){
[segue.destinationViewController setTitle:#"Compensation"];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *keys = [[NSMutableArray alloc] init];
NSMutableDictionary *contents = [[NSMutableDictionary alloc] init];
NSString *workKey = #"work";
NSString *absKey = #"absence";
NSString *comKey = #"compensation";
[contents setObject:[NSArray arrayWithObjects:#"Work Time", nil] forKey:workKey];
[contents setObject:[NSArray arrayWithObjects:#"Absence", nil] forKey:absKey];
[contents setObject:[NSArray arrayWithObjects:#"Compensation", nil] forKey:comKey];
[keys addObject:workKey];
[keys addObject:absKey];
[keys addObject:comKey];
[self setSectionKeys:keys];
[self setSectionContents:contents];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
[[cell textLabel] setText:contentForThisRow];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_selectedIndex = indexPath.row;
[self.tableView reloadData];
//case1
[self performSegueWithIdentifier:#"WorkTime" sender:self];
//case2
[self performSegueWithIdentifier:#"Absence" sender:self];
//case3
[self performSegueWithIdentifier:#"Compensation" sender:self];
}

I think your problem is here
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Your custom cell should have a unique cell identifier. You need to pass that unique cell identifier to -dequeueReusableCellWithIdentifier:.
What have you used as your cell identifier?

you should use NSLOG to be sure that you are going to different views, I think you are always in a same view is the reason that you cann't see the new changes

Related

multiple checkmarks from

Tutorial I am following: http://www.appcoda.com/ios-programming-tutorial-create-a-simple-table-view-app/
I have created a tableview with 16 cells. When I select a row, it will show checkmark on it.
But when I scroll the tableview, there is also a checkmark showing on another cell further down the list. This repeats for any cell selected.
#import "FlightChecklistViewController.h"
#interface FlightChecklistViewController ()
#end
#implementation FlightChecklistViewController
{
NSArray *tableData;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Initialize table data
tableData = [NSArray arrayWithObjects:#"Egg Benedict", #"Mushroom Risotto", #"Full Breakfast", #"Hamburger", #"Ham and Egg Sandwich", #"Creme Brelee", #"White Chocolate Donut", #"Starbucks Coffee", #"Vegetable Curry", #"Instant Noodle with Egg", #"Noodle with BBQ Pork", #"Japanese Noodle with Pork", #"Green Tea", #"Thai Shrimp Cake", #"Angry Birds Cake", #"Ham and Cheese Panini", nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}
- (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];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIAlertView *messageAlert = [[UIAlertView alloc]
initWithTitle:#"Row Selected" message:#"You've selected a row" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
// Display Alert Message
[messageAlert show];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#end
Any suggestions?
You need to store the information about the rows indexpaths, that were selected, somehow.
And populate your cell according to it.
#import "ViewController.h"
#interface ViewController ()
#property (nonatomic, strong) NSMutableArray *selectedCells;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.selectedCells = [NSMutableArray array];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 100;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *unifiedID = #"aCellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:unifiedID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:unifiedID];
}
cell.textLabel.text = [NSString stringWithFormat:#"%u", indexPath.row];
//if the indexPath was found among the selected ones, set the checkmark on the cell
cell.accessoryType = ([self isRowSelectedOnTableView:tableView atIndexPath:indexPath]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
//if a row gets selected, toggle checkmark
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if([self isRowSelectedOnTableView:tableView atIndexPath:indexPath]){
[self.selectedCells removeObject:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
[self.selectedCells addObject:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
-(BOOL)isRowSelectedOnTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath
{
return ([self.selectedCells containsObject:indexPath]) ? YES : NO;
}
#end
you will find the complete example code on github
The problem is that cells are reused. So, if you add a checkmark accessory view to a cell further up it'll appear again when the cell is reused further down. You should save which ones are checkmarked in an array somewhere that correlates to the rows of the table when you add/remove a checkmark. Then, when you give the table view a new cell you can determine whether or not it needs a checkmark and set that up.
I had the same issue recently with one of my apps, and I fixed it by doing this:
#property (nonatomic, strong) NSArray *list;
- (void)viewDidLoad
{
[super viewDidLoad];
self.list = [[NSArray alloc] initWithObjects:#"foo", #"bar", nil];
}
- (NSString *)SettingsPlist
{
NSString *paths = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *PlistPath = [paths stringByAppendingPathComponent:#"Settings.plist"];
return PlistPath;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [[self list] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *contentForThisRow = [[self list] objectAtIndex:[indexPath row]];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:[self SettingsPlist]];
NSString *row = [NSString stringWithFormat:#"%d",indexPath.row];
if([[dict objectForKey:row]isEqualToString:#"0"])
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
[[cell textLabel] setText:contentForThisRow];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSMutableDictionary *plist = [NSMutableDictionary dictionaryWithContentsOfFile:[self SettingsPlist]];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *row = [NSString stringWithFormat:#"%d",indexPath.row];
if(cell.accessoryType == UITableViewCellAccessoryNone)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
NSString *on = #"1";
[plist setObject:on forKey:row];
[plist writeToFile:[self SettingsPlist] atomically:YES];
}
else if(cell.accessoryType == UITableViewCellAccessoryCheckmark)
{
cell.accessoryType = UITableViewCellAccessoryNone;
NSString *off = #"0";
[plist setObject:off forKey:row];
[plist writeToFile:[self SettingsPlist] atomically:YES];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

How to delegate an open source tableview badge to UITableview

I know similar questions have been asked few times about how to add badge to tableviewcell, but I could not make it working
Basically what I want is to show user a simple notification either a red number at the right part of the table view cell or a rectangle or like native email app.
So I have tried both of this two source code TDbadgcell and DDbadgecell
Now the problem is I can not delegate them, I have to import their .h classes and call either one of the below functions in my table view
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
TDBadgedCell *cell = [[TDBadgedCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
or
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
DDBadgeViewCell *cell = (DDBadgeViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[DDBadgeViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
But when I do that my tableView didSelectRowAtIndexPath: and (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender methods are not working, I can click the rows but they stay higlihted blue nothing happens also my arrow at the right side of the table is dissappears.
So how can I achieve to add a badge to table view cell row either with above source codes or any other methods?
EDIT:::
After putting NSLOG I can see that did select row is called but perform segue still does not work. Without adding any of the above code it works perfect.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//static NSString *CellIdentifier = #"MeetingCell";
static NSString *CellIdentifier = #"Cell";
DDBadgeViewCell *cell = (DDBadgeViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[DDBadgeViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSString *description =#":";
NSString *name =#"";
NSString *fileStatus=#"";
name = [[self agenda] getFileNameWithSection:[indexPath section] Row:[indexPath row]];
description = [[self agenda] getFileDescriptionWithSection:[indexPath section] Row:[indexPath row]];
fileStatus = [[self agenda] getFileStatusWithFileName:name];
NSString * cellLabel = [NSString stringWithFormat:#" %# : %#",description,name];
//alloc row images
UIImage *docImage = [UIImage imageNamed:#"ICON - Word#2x.png"];
UIImage *xlsImage = [UIImage imageNamed:#"ICON - Excel#2x.png"];
// UIImage *picImage = [UIImage imageNamed:#"ICON - Image#2x.png"];
UIImage *pdfImage = [UIImage imageNamed:#"pdf icon#2x copy.png"];
UIImage *pptImage = [UIImage imageNamed:#"ICON - PPT#2x.png"];
//Determine what status to display for a file
//No need to that since wee use push notification
if ([fileStatus isEqualToString:#"new"]){
cellLabel = [NSString stringWithFormat:#"%# (%#)",cellLabel,#"New"];
cell.badgeText = [NSString stringWithFormat:#"Update"];
cell.badgeColor = [UIColor orangeColor];
}else if ([fileStatus isEqualToString:#"outdated"]){
cellLabel = [NSString stringWithFormat:#"%# (%#)",cellLabel,#"Outdated"];
cell.badgeText = [NSString stringWithFormat:#"Update"];
cell.badgeColor = [UIColor orangeColor];
}else if ([fileStatus isEqualToString:#"updated"]){
cellLabel = [NSString stringWithFormat:#"%# (%#)",cellLabel,#"Latest"];
}
UIFont *font1 = [UIFont fontWithName:#"Century Gothic" size:15.0f];
cell.textLabel.font=font1;
//if there is no file user can not tocuh the row
if ([name length]==0) {
cell.userInteractionEnabled = NO;
cell.accessoryType = UITableViewCellAccessoryNone;
cell.textLabel.text = description;
}else{
//set cell title
cell.textLabel.text = cellLabel;
}
//set row images
if ([name rangeOfString:#"docx"].location != NSNotFound) {
cell.imageView.image= docImage;
}else if ([name rangeOfString:#"xlsx"].location != NSNotFound){
cell.imageView.image= xlsImage;
}
else if ([name rangeOfString:#"pdf"].location != NSNotFound){
cell.imageView.image= pdfImage;
}
else if ([name rangeOfString:#"ppt"].location != NSNotFound){
cell.imageView.image= pptImage;
}
cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"rounded corner box center#2x.png"]];
// At end of function, right before return cell
cell.textLabel.backgroundColor = [UIColor clearColor];
NSLog(#"%#",cell.textLabel.text);
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];.0
*/
NSLog(#"didselect row is called %d",indexPath.row);
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[DBSession sharedSession] isLinked]) {
if([[segue identifier] isEqualToString:#"pushDocumentView"])
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSInteger section =[indexPath section];
NSInteger row = [indexPath row];
NSString *fileName = [[self agenda] getFileNameWithSection:section Row:row];
NSDictionary * agendaDic = [[[self agenda]revision] objectForKey:fileName];
NSString *fileStatus =[agendaDic objectForKey:#"status"];
DocumentViewController *docViewController = [segue destinationViewController];
//This will display on the Document Viewer
docViewController.fileName=fileName;
//This will determine remote or local copy display
docViewController.fileStatus=fileStatus;
}
}else {
[self displayError];
[self setWorking:NO];
}
}
Just called a perfromseguewith Identifier
because this is called - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender also when you call [self performSegueWithIdentifier:#"yoursegue" sender:self];
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];.0
*/
[self performSegueWithIdentifier:#"yoursegue" sender:self];
NSLog(#"didselect row is called %d",indexPath.row);
}

adding check box to one of sections in UITableView

I create a table view controller programmatically that contains different sections I want to add 3 rows with check mark box in my third sections (I used storyboard!)
would you please give me some hint that how can I do that ..
my question is how can I set checkbox in left side for my third sections
here is the picture:instead of Please set your code: having 3 rows with check mark box in Absence Code sections
Here is my view in storyboard:
Here is the code:
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *keys = [[NSMutableArray alloc] init];
NSMutableDictionary *contents = [[NSMutableDictionary alloc] init];
NSString *staKey = #"Start";
NSString *endKey = #"End";
NSString *absKey= #"Absence";
[contents setObject:[NSArray arrayWithObjects:#"Time: 08:00 Date: Fri,3 Aug, 2012", nil] forKey:staKey];
[contents setObject:[NSArray arrayWithObjects:#"Time: 17:57 Date: Fri,3 Aug, 2012", nil] forKey:endKey];
[contents setObject:[NSArray arrayWithObjects:#"Please set your code", nil] forKey:absKey];
[keys addObject:staKey];
[keys addObject:endKey];
[keys addObject:absKey];
[self setSectionKeys:keys];
[self setSectionContents:contents];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
[[cell textLabel] setText:contentForThisRow];
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSMutableArray *weekArray = [[ NSMutableArray alloc] initWithObjects: #"Start Time", #"End Time",#"Absence Code",
nil];
return [weekArray objectAtIndex:section];
}
- (UITableViewCellAccessoryType)tableView:(UITableView *)tv accessoryTypeForRowWithIndexPath:(NSIndexPath
*)indexPath {
return UITableViewCellAccessoryDisclosureIndicator;
}
Thanks in advance!
Each checkbox cell should be a Custom TableViewCell. Then simply drop an ImageView and a Label in the TableViewCell and use didSelectRowAtIndex: to toggle the image between checked/unchecked.
EDIT:
Check Selecting multiple rows of a UITableView link get helped.
Do this: change code in below method:
- (UITableViewCellAccessoryType)tableView:(UITableView *)tv accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath {
if(indexPath.section ==2)
{
return UITableViewCellAccessoryCheckMark;
}
else
{
return UITableViewCellAccessoryDisclosureIndicator;
}
}

customising UITableViewCell with picture objective-C

I want to have custom cell in UITableView, I create my UIView via storyboard and I linked them to the code
I don't know why my picture does not appear
Would you please check my code ?
customise code .h
: UITableViewCell
#property (strong, nonatomic) IBOutlet UIImageView *weekImg;
#end
customise code .m
#synthesize weekImg;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
weekImg=[[UIImageView alloc]initWithImage:[UIImage imageNamed:#"red.png"]];
}
return self;
}
My method: cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
WeekTableViewCell *cell = (WeekTableViewCell*)[tableView dequeueReusableCellWithIdentifier:#"WeekTableViewCell"];
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];
[[cell textLabel] setText:contentForThisRow];
return cell;
}
I think this is because your code is just looking for previosuly created cells. at th start no cells will be created and wont be able to retrieve any this way.
WeekTableViewCell *cell = (WeekTableViewCell*)[tableView dequeueReusableCellWithIdentifier:#"WeekTableViewCell"];
if (cell == nil){
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"WeekTableViewCell" owner:nil options:nil];
for(id currentObject in topLevelObjects)
{
if([currentObject isKindOfClass:[WeekTableViewCell class]])
{
cell = (WeekTableViewCell *)currentObject;
break;
}
}
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];
[[cell textLabel] setText:contentForThisRow];
return cell;
this way create a new cell if there isnt a cell to re use
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.imageView.layer.cornerRadius=10.0f;
[cell.imageView.layer setMasksToBounds:YES];
cell.textLabel.text=[NSMUtableArray objectAtIndex:indexPath.row];
switch (indexPath.row)
{
case 0:
cell.imageView.image=[UIImage imageNamed:#"img1.png"];
break;
case 1:
cell.imageView.image=[UIImage imageNamed:#"img2.png"];
break;
case 2:
cell.imageView.image=[UIImage imageNamed:#"img3.png"];
break;
case 3:
cell.imageView.image=[UIImage imageNamed:#"img4.png"];
break;
default:
break;
}
}
cell.imageView.layer.cornerRadius=10.0f;
[cell.imageView.layer setMasksToBounds:YES];, it is used by QuartzCore.
The output of the cell like this, we can adjust the image size.

how to use custom cell in objective-c

I want to create 4 cell "button or picture" in one row in uiTableView like this picture:
but I don't know how can I do that :
would you please help me!
Thanks in advance!
here is my code :
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *keys = [[NSMutableArray alloc] init];
NSMutableDictionary *contents = [[NSMutableDictionary alloc] init];
NSString *monKey = #"Monday";
NSString *tueKey = #"Tuesday";
NSString *wedKey = #"Wednday";
NSString *thuKey = #"Thusday";
NSString *friKey = #"Friday";
NSString *satKey = #"Satuarday";
NSString *sunKey = #"Sunnday";
[contents setObject:[NSArray arrayWithObjects:#"Work Time", #"Absence", nil] forKey:monKey];
[contents setObject:[NSArray arrayWithObjects:#"Compensation", #"Work Time", #"Absence", nil] forKey:wedKey];
[contents setObject:[NSArray arrayWithObjects:#"Compensation", #"Work Time", nil] forKey:tueKey];
[contents setObject:[NSArray arrayWithObjects:#"Compensation", #"Work Time", nil] forKey:thuKey];
[contents setObject:[NSArray arrayWithObjects:#"Compensation", #"Work Time", nil] forKey:friKey];
[contents setObject:[NSArray arrayWithObjects:#"Compensation", #"Work Time", nil] forKey:satKey];
[contents setObject:[NSArray arrayWithObjects:#"Compensation", #"Work Time", nil] forKey:sunKey];
[keys addObject:tueKey];
[keys addObject:monKey];
[keys addObject:wedKey];
[keys addObject:thuKey];
[keys addObject:friKey];
[keys addObject:satKey];
[keys addObject:sunKey];
[self setSectionKeys:keys];
[self setSectionContents:contents];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self action:#selector(addNewItem)];
self.navigationItem.rightBarButtonItem = rightButton;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
[[cell textLabel] setText:contentForThisRow];
int column = 4;
for (int i=0; i<column; i++) {
UIImageView *aBackgroundImageView = [[UIImageView alloc]initWithFrame:CGRectMake(32+184*i,10, 167,215)];
aBackgroundImageView.tag = (column*indexPath.row)+i;
[cell.contentView addSubview:aBackgroundImageView];
// [aBackgroundImageView release];
}
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if(section == 0)
return #"Monday";
else if(section == 1){
return #"Tuesday";
}else if(section == 2){
return #"Wednesday";
} else if(section == 3){
return #"Thuesday";
} else if(section == 4){
return #"Friday";
} else if(section == 5){
return #"Saturday";
}else
return #"Sunday";
}
Edit 1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithFrame:CGRectZero];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
int column = 4;
for (int i=0; i<column; i++) {
UIImageView *aBackgroundImageView = [[UIImageView alloc]initWithFrame:CGRectMake(32+184*i,10, 167,215)];
aBackgroundImageView.tag = (column*indexPath.row)+i;
[cell.contentView addSubview:aBackgroundImageView];
}
return cell;
}
You can design a custom cell with 4 button in Xcode and map them to tableview
If you don't need user interaction, you can add them to a UIView (each is a subview with an appropriate frame), and make the container view the cell's backgroundview.
You can add the views directly to the cell's contentView and get user interaction, but you have to be careful as that area gets modified if there is a accessoryView etc.
It will be easier to design the custom cell and lay out your 4 buttons (since you need user interaction) in interface builder. Here's a very good tutorial to get you started. Give a tag number to each of the button so you will know which button was tapped later.
want to create 4 cell "button or picture" in one row in uiTableView like this picture:
but I don't know how can I do that :
First of all, you will need to create an objective-c class called MyCell. MyCell will be a subclass of UITableViewCell (this is very important)
In your MyCell.h,
#interface MyCell : UITableViewCell
#property (nonatomic, weak) IBOutlet UIButton *firstButton;
#property (nonatomic, weak) IBOutlet UIButton *secondButton;
#property (nonatomic, weak) IBOutlet UIButton *thirdButton;
#property (nonatomic, weak) IBOutlet UIButton *fourthButton;
#end
Then in your MyCell.m, just synthesize all those buttons.
In your tableview, where you want to use your customized cell, you need to modify the cellForRowAtIndexPath method. Replace your UITableViewCell with your custom MyCell. And don't forget to import "MyCell.h" into that tableview that you are using.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell)
{
cell = [[MyCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
[cell.firstButton setTintColor:[UIColor redColor];
[cell.secondButton setTintColor:[UIColor redColor];
// and so on.. til you set all your four buttons.
return cell; }
And from there, your tableview should be showing your custom cell.