TableView sections don't seem to work Objective - C - ios

The problem I am encountering is that I don't know how to code my tableViewController properly, so it displays 2 sections, one with Items (object type) which value is higher than 50, and second section for Items valued less than 50. My code looks like this:
#implementation ItemsViewController
- (instancetype)init
{
self = [super initWithStyle:UITableViewStylePlain];
if(self)
{
for (int i = 0; i < 5; i++)
{
[[ItemStore sharedStore] createItem];
}
}
return self;
}
- (instancetype)initWithStyle:(UITableViewStyle)style
{
return [super initWithStyle:UITableViewStylePlain];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"UITableViewCell"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView;
{
return #[#">50", #"<=50"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)
{
return [[[ItemStore sharedStore] itemsValuedOver50] count];
}
else
{
return [[[ItemStore sharedStore] itemsValuedBelowOrEqual50] count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"UITableViewCell" forIndexPath:indexPath];
if (indexPath.section == 0)
{
NSArray *items = [[ItemStore sharedStore] itemsValuedOver50];
NSArray *item = items[indexPath.row];
cell.textLabel.text = [item description];
}
else
{
NSArray *items = [[ItemStore sharedStore] itemsValuedBelowOrEqual50];
NSArray *item = items[indexPath.row];
cell.textLabel.text = [item description];
}
return cell;
}
#end
Section names (">50" and <=50) appear in the middle of the right side of the screen(looks like it is out of the table view), and the table view still behaves as it had only one section. These 5 items are not sorted in any way and I have no idea why.
Here is the link to screenshot of the application: http://imgur.com/3MoHmmn
I've been looking for answer for some time, but to be fair i don't know how to describe my problem well enough to find any solution online, thats why I'm creating a new topic.
Thanks for your help in advance.
EDIT: I've figured out the problem. It took me few days and it was preety stupid. I used wrong method. Instead of using sectionIndexTitlesForTableView: i should have used titleForHeaderInSection:. Now everything works as i wanted it to work.

Try setting the section height via the delegate
- (CGFloat)tableView:(UITableView *)tableView heightForSection:(NSUInteger)section;

Related

obj c different table cell names in a table

I'm trying to have table cells with different names, but so far they all have the text '1'.
How do I make the cells each have a unique name, as described in the tableData array?
- (void)viewDidLoad {
[super viewDidLoad];
// self.clearsSelectionOnViewWillAppear = NO;
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
tableData = [NSMutableArray arrayWithObjects:#"1", #"2", #"333", nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section {
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"subTable"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"CELL"];
}
cell.textLabel.text = [tableData objectAtIndex:0];
return cell;
}
I heard that I might need to use a method 'forIndex' or 'indexPath' or some such?
Change this line:
cell.textLabel.text = [tableData objectAtIndex:0];
to this:
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
Not to be rude, just a thought: if this is not obvious to you, you should probably start with some beginner level tutorials.

Static Cells - Hiding using the heightforRowatIndexPath method

Hi guy I'm trying to hide some of the rows in section 1 (Second section) depending on what type of feedback the user has selected:
I'm using static cells but at the moment nothing is being removed when I select one of the options in the TypeVC. There are no errors at the moment but having a guess I think its something to do with the logical operators I'm using in the switch statement. Sorry for dumping my code but as I'm very new to IOS I don't know what exactly you guys would need to see.
[1]
if (variable == (1|2|3)){}
I'm used to Java and I use this kind statement quite frequently as it saves writing. Is this how to do it in objective-c?
[2]
Where and how have I gone wrong here trying to get the cells to dissapear?
FeedbackTableViewController:
#import "FeedbackTableViewController.h"
#import "TypeTableViewController.h"
#interface FeedbackTableViewController ()
#property NSInteger index;
#end
#implementation FeedbackTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidAppear:(BOOL)animated{
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (NSIndexPath *) tableView:(UITableView *)tableView
willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Type: %i",_type);
if (indexPath.section == 0 && indexPath.row == 0)
[self performSegueWithIdentifier:#"showTypeVC" sender:self];
return indexPath;
}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
TypeTableViewController *tvc = [segue destinationViewController];
tvc.indexchoosen = _index;
}
//- (UITableViewCell *)tableView:(UITableView *)tableView
// cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//
// UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
//
// if (indexPath.row==0) cell.textLabel.text = _typeString;
// else if (indexPath.row)
//
// return cell;
//}
- (CGFloat) tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(#"section: %i row:%i",indexPath.section, indexPath.row);
if (indexPath.section == 1) {
switch (_type) {
case 0:
if (indexPath.row==(2|3|4))return 0;
break;
case 1:
if (indexPath.row==(0|1|4))return 0;
break;
case 2:
if (indexPath.row==(0|1|2|3))return 0;
break;
case 3:
return 0;
break;
case 4:
return 0;
break;
case 5:
return 0;
break;
default:
return 0;
break;
}
}
return 43;
}
- (IBAction)unwindtypeVC:(UIStoryboardSegue *)segue { }
#end
TypeTableViewController:
#import "TypeTableViewController.h"
#import "FeedbackTableViewController.h"
#interface TypeTableViewController ()
#end
#implementation TypeTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_typeoptions = #[#"Routing Issues",
#"Wrongly Labelled Location",
#"Missing Location",
#"Comment on Useability",
#"Suggestions",
#"General Feedback"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 6;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.textLabel.text = self.typeoptions[indexPath.row];
return cell;
}
- (NSIndexPath *)tableView:(UITableView *)tableView
willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
_indexchoosen = indexPath.row;
return indexPath;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
NSString *string = _typeoptions[_indexchoosen];
FeedbackTableViewController *fvc1 = [segue destinationViewController];
fvc1.typeString.text = _typeoptions[_indexchoosen];
fvc1.type = _indexchoosen;
}
#end
I'm open to better ideas on achieving what I want to achieve also so if you would consider telling me a more efficient way of doing this I would be grateful. I know delegates might be an option however I'm not confident with them yet and would thought this would be easier for me.
For [1], try this and see it yourself:
int test = 3;
if(test == (1 | 2))
NSLog(#"_MEH_");
Since it's bitwise OR operation, 0010 | 0001 equals to 0011, which is equal to 3. Hence, I would not advise you to use an operation like that. (If that's not intentional, of course).
For [2], you should use deleteRowsAtIndexPaths:withRowAnimation: call for UITableView in order to delete rows.
For example;
[self.tableView beginUpdates];
NSIndexPath* rowToDelete = [NSIndexPath indexPathForRow:0 inSection:0]; // For showing purposes only.
NSArray* indexArray = [NSArray arrayWithObjects:rowToDelete, nil];
[self.tableView deleteRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationMiddle];
[self.tableView endUpdates];
Also, don't forget to update your data source accordingly. You may want to remove some objects from
self.typeoptions
array.
P.S: Another addition, you should also change tableView:numberOfRowsInSection: since there will be less rows than 6.
I actually managed to use this method of changing the row heights to 0.
In order to do it effectively I had to to remove the placeholder/any initial text in the rows that I didn't want shown. This required some storyboard connections which you will see named as _feedbackText _startLocation etc. When a user selected a new row, they would perform a segue to the original feedback form and therefore the viewDidAppear was called. I used this to call the [self.tableView reloadData]. Originally the change in the variable _type would not actually change anything but the heightForRowAtIndexPath is recalled when the data is reloaded.
I'm sure that using the delete row at indexPath would have worked also but I wanted to store the information that the user may have typed before they changed the type of feedback.
The New Method:
- (CGFloat) tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSInteger i = indexPath.row;
if (indexPath.section == 1) {
switch (_type) {
case 0:
_startLocation.placeholder = #"Start Location:";
_destination.placeholder = #"Destination:";
_locationName.placeholder = #"";
_correctName.placeholder = #"";
_missingLocation.placeholder = #"";
if (i==2||i==3||i==4) return 0;
break;
case 1:
_startLocation.placeholder = #"";
_destination.placeholder = #"";
_locationName.placeholder = #"Location Name:";
_correctName.placeholder = #"Correct Name:";
_missingLocation.placeholder = #"";
if (i==0||i==1||i==4)return 0;
break;
case 2:
_startLocation.placeholder = #"";
_destination.placeholder = #"";
_locationName.placeholder = #"";
_correctName.placeholder = #"";
_missingLocation.placeholder = #"Missing Location:";
if (i==0||i==1||i==2||i==3)return 0;
break;
case 3:
return 0;
break;
case 4:
return 0;
break;
case 5:
return 0;
break;
default:
_startLocation.placeholder = #"";
_destination.placeholder = #"";
_locationName.placeholder = #"";
_correctName.placeholder = #"";
_missingLocation.placeholder = #"";
if (i==0||i==1||i==2||i==3||i==4)return 0;
break;
}
} else if (indexPath.section==2 && indexPath.row==2) return 240;
else if (indexPath.section==0 && indexPath.row==0) return 40;
return 30;
}
This will essentially hide but not get rid of the information in the text fields. This is very useful if you want to the keep any information the user typed in.
I hope this helps anyone trying to hide rows in a grouped static table view controller.

NSMutableArray With UITableViewCell

I'm having an issue with my UITableviewCell, it generates only the first index inside of my NSMutableArray. I've used NSLog(#"Count %i %#", [enterPrise_names count], enterPrise_names);
To check the number of objects inside of my NSMutableArray everything seems fine with that.
I've already wired up everything including the UItableViewDataSource, UITableViewDelege, and Matching up the cell identifier of the TableViewCell.
The problem is that two of my labels only show the first object inside of my NSMutableArrays.
I want to post the photos of my simulation to you guys but this is my first post of Stackoverflow and I don't have enough reputation to do that task.
well it looks like this
Fuji Siam
Fuji Siam
Fuji Siam
Fuji Siam
Fuji Siam
Fuji Siam
Here is my code
#import "MyQueueViewController.h"
#import "QueueCell.h"
#interface MyQueueViewController ()
#end
#implementation MyQueueViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidAppear:(BOOL)animated
{
}
- (void)viewDidLoad
{
[super viewDidLoad];
enterPrise_names = [[NSMutableArray alloc]initWithObjects:#"Fuji",#"MK",#"KFC",#"PizzaHut",#"McDonal",#"BurgerKing", nil];
BranchName = [[NSMutableArray alloc]initWithObjects:#"Siam",#"Paragon", #"CTW", #"Pinklao", #"Bangkae", #"Bangna", nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [enterPrise_names count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"QueueCell";
QueueCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[QueueCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
NSLog(#"Count %i %#", [enterPrise_names count], enterPrise_names);
cell.txtBranch.text = [BranchName objectAtIndex:indexPath.row];
cell.txtShop.text = [enterPrise_names objectAtIndex:indexPath.row];
return cell;
}
I would be very appreciated if any of you guys would point out my mistakes.
Thank you very much for your help.
Your tableview contains many sections but but only one row by section. Try this :
cell.txtBranch.text = [BranchName objectAtIndex:indexPath.section];
cell.txtShop.text = [enterPrise_names objectAtIndex:indexPath.section];
However, if you don't actually need many sections, you should probably set the number of rows according to your array and inverse numbers of rows and sections in here :
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [enterPrise_names count];
}
Try,
Change cellForRowAtIndexPath: method to
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
....
NSLog(#"Count %i %#", [enterPrise_names count], enterPrise_names);
cell.txtBranch.text = [BranchName objectAtIndex:indexPath.section];
cell.txtShop.text = [enterPrise_names objectAtIndex:indexPath.section];
return cell;
}
Since each section has one row, indexPath.row will be zero in all case. So only first element of both BranchName and enterPrise_names array will be shown in labels. Changing indexPath.row to indexPath.section will solve the problem

A uitable view with 3 groups in it

i am a developing a ios app with a view controller and a table view in it. i am trying to load list of items in 3 groups but when i compile it it shows correct count but not showing all the items jus repeating items. please help. let me post my code here.
#interface ViewController ()
#end
#implementation ViewController {
NSArray *menuItems;
NSArray *menuItems2;
NSArray *dash;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithWhite:0.6f alpha:1.0f];
self.tableView.backgroundColor = [UIColor colorWithWhite:0.6f alpha:1.0f];
self.tableView.separatorColor = [UIColor colorWithWhite:0.15f alpha:0.2f];
menuItems = #[#"itm1", #"itm2", #"itm3", #"itm4"];
menuItems2 = #[#"itm1", #"itm2", #"itm3", #"itm4"];
dash = #[#"itm1", #"itm2"];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
if (section == 0) {
return [menuItems count];
}
if (section == 1) {
return [menuItems2 count];
}
if (section == 2) {
return [dash count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
return cell;
}
#end
Your cellForRowAtIndexPath... method needs to be written so the cell is populated with proper data based on the cell's section and row. Right now you don't do anything but use an empty cell.
You don't configure your cells. After dequeuing a cell, you have to set its title.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Get the correct array, depending on the current section.
NSArray *items = nil;
if (indexPath.section == 0) {
items = menuItems;
}
else if (indexPath.section == 1) {
items = menuItems2;
}
else if (indexPath.section == 2) {
items = dash;
}
// Get the string from the array and set the cell's title
NSString *title = items[indexPath.row];
cell.textLabel.text = title;
return cell;
}

Give section index without any section in table view in objective C for sorting single NSArray?

means I want all that records from NSArray starts with 'J' when i tapped on 'J' section title in right side.
Below i give my code in this code i don't want any section i have only one plist
in that i store all the states in india so i now i don't want to any section and when i
tap on any alphabet at that time sorting perform and all that rows which starts with that tapped
alphabet i want. i give example above and code below
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(#"Enter in numberOfRowsInSection method.");
NSLog(#"returning number of rows in a plist.==%d ",test.count);
return test.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"Enter cellForRowAtIndexPath method.");
NSLog(#"Creating a cell..");
//Create a cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
//The above line used to reuse the memory in a tableView only allocate memory which is displayed at a time.
if(cell == nil){
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
NSLog(#"Filling Cell Content..");
//Fill it with Contents
cell.textLabel.text = [test objectAtIndex:indexPath.row];
NSLog(#"Cell content === %#",cell.textLabel.text);
NSLog(#"Exit cellForRowAtIndexPath method.");
//return it
return cell;
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
NSArray *secTitle = [[NSArray alloc]initWithObjects:#"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];
return secTitle;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"In viewDidLoad method");
//Load from the plist File.
NSString *str = [[NSBundle mainBundle] pathForResource:#"groupedTest" ofType:#"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:str];
NSLog(#"dict === %#",dict);
test = [[NSArray alloc]initWithArray:[dict objectForKey:#"statesOfIndia"]];
//NSLog(#"In viewDidLoad method & Array fill from plist");
NSLog(#"Array count(Length) starts from 1 not 0 = %d",test.count);
NSLog(#"Exit from viewDidLoad method..");
// Do any additional setup after loading the view, typically from a nib.
}
In this test is my NSArray declared in .h file. And please tell me the answer because i start learning before 5 days only so i'm totally new in iPhone and objective C. Thank You Very Much!!
There is an option in UITableView for this use case, implement the UITableViewDataSource delegate methods
-sectionIndexTitlesForTableView: and
-tableView:sectionForSectionIndexTitle:atIndex:
For customising section titles and supporting localisation,
UILocalizedIndexedCollation need to be used. You can read about it here
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
}
- (NSInteger)tableView:(UITableView *)tableView
sectionForSectionIndexTitle:(NSString *)title
atIndex:(NSInteger)index
{
return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}
You will need to implement the below method to handle user touch on a section title
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return [secTitle indexOfObject:title];
}

Resources