NSMutableArray With UITableViewCell - ios

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

Related

TableView sections don't seem to work Objective - C

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;

How to reduce size of the UITableViewCell at runtime in Objective C?

I have made a custom cell in the storyboard having height 300, which include label and imageView, but for some case if I had not received image from the response I want to decrease the cell height and want to remove the imageView from the cell so that it could not be visible to the user .
In that case I just want to show the text in the label.
How could I achieve that? I know its silly question but I am stuck here.
Your help would be appreciated.
Thanks
I have wrote some basic code fro your understanding how table view cell resizing works (irrelevant of custom or basic cells).
#interface MasterViewController ()
#property NSArray *objects;
#end
#implementation MasterViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_objects = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"ImageList" ofType:#"plist"]];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _objects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSDictionary *object = _objects[indexPath.row];
cell.textLabel.text = object[#"text"];
cell.imageView.image = [UIImage imageNamed:object[#"image"]];
return cell;
}
//- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
// return _objects.allKeys[section];
//}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return [_objects[indexPath.row][#"image"] length] ? 60.0f : 30.0f;
}
#end
In above code you prime focus should be on method heightForRowAtIndexPath and it's code line return [_objects[indexPath.row][#"image"] length] ? 60.0f : 30.0f;.
Now as if you load image dynamically from server then keep track for image of which cell is been loaded and call [tableView reloadRowsAtIndexPaths:<#(nonnull NSArray<NSIndexPath *> *)#> withRowAnimation:<#(UITableViewRowAnimation)#>] with relevant index path. Also make sure you update image loaded information in your dataSource i.e. _objects in above code.
I have uploaded code here. Check UITableViewTest in workspace.

Recreate a uitableview section dynamically on a uibutton`s click

I need to recreate a UITableView section on a UIButton click. Here is an example for this.
AND in button click it should be created like this.
But i am not able to implement this .
Please Help.
I am just creating a simple UITableView with a textLabel.
Code For cellForRowAtIndexPath is
- (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;
}
Create a integer variable index and property for tableview in .h
#property UITableView *tableView;
Then in -(void)viewDidLoad set index=0;
Now, write a method which is get called when you tap on add button
-(void)newTableView
{
tableView = [UITableView alloc] initWithFrame:CGRectMake(x,y+height*index,width,height)];
//other code related to table
tableView.tag = ++index;
[self.view addSubview:tableView];
}
Use tag in delegate methods
Edit: As you requested to add new section in Table View instead of new table view
You have to first create table view of grouped style
tableView = [UITableView alloc] initWithFrame:(CGRect)frame style:UITableViewStyleGrouped];
Now, When you hit on that add button call a method
-(void)newSection
{
count++;
[tableView reloadData];
}
Then, you have to implement this delegate methods for grouped table view
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return #"title";
}
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
//same as your previous code
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//same as your previous code
}
//
// ViewController.m
// DynamicNewSections
//
// Created by J Pulikkottil on 14/07/15.
// Copyright (c) 2015 J Pulikkottil. All rights reserved.
//
#import "ViewController.h"
#interface ViewController ()
#property(nonatomic) NSMutableArray *arrayData;
#property (strong, nonatomic) IBOutlet UITableView *tableViewTest;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.arrayData = [NSMutableArray array];
//section1
[self addSectionArray];
}
- (IBAction)addSection:(UIButton *)sender {
[self addSectionArray];
[self.tableViewTest reloadData];
}
- (void) addSectionArray{
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:#"Height",#"label", #"", #"value", nil];
NSArray *arraysection = [NSArray arrayWithObjects:dict, nil];
[self.arrayData addObject:arraysection];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableViewTest.frame.size.width, 45)];
view.backgroundColor = [UIColor yellowColor];
return view;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return [self.arrayData count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [[self.arrayData objectAtIndex:section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CellDetails" forIndexPath:indexPath];
NSArray *arraysection = [self.arrayData objectAtIndex:indexPath.section];
NSDictionary *dict = [arraysection objectAtIndex:indexPath.row];
cell.textLabel.text = [dict valueForKey:#"label"];
cell.detailTextLabel.text = [dict valueForKey:#"value"];
return cell;
}
#end
Try this:
Just have a property (e.g. numberOfSections)
Set it to 1 in viewDidLoad
In the numberOfSectionsInTableView: return self.numberOfSections;
In the IBAction of your UIButton add this code:
self.numberOfSections++;
[self.tableView beginUpdates];
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:self.numberOfSections-1]
withRowAnimation:UITableViewRowAnimationBottom];
[self.tableView endUpdates];

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

How to make uitableview with multiple sections dynamically?

How to make uitableview with multiple sections dynamically with dynamic array count ?
Check this long code, I think this will come handy
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#pragma mark TableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [self.words count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [[self.words objectAtIndex:section]count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:#"cell"];
// if (cell==nil) {
// cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"];
// }
//UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:#"cell"];
//if (cell==nil) {
UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"];
//}
cell.textLabel.text=[[self.words objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
cell.detailTextLabel.text=[NSString stringWithFormat:#"This is detailed subtitle for %#.",cell.textLabel.text];
//cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
NSString *imagePath=[[NSBundle mainBundle]pathForResource:#"images" ofType:#"jpeg"];
UIImage *image=[[UIImage alloc]initWithContentsOfFile:imagePath];
cell.imageView.image=image;
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [self.alphabets objectAtIndex:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;{
return self.alphabets;
}
#pragma mark -
#pragma mark Default
- (void)viewDidLoad
{
self.alphabets=[NSMutableArray new];
for (char i=97; i<123; i++) {
[self.alphabets addObject:[[NSString stringWithFormat:#"%c",i]capitalizedString]];
}
self.words=[NSMutableArray new];
for (NSString *character in _alphabets) {
NSMutableArray *twoD=[NSMutableArray new];
for (int i=1; i<(rand()%10)+1; i++) { //fill randamly any number 1 to 10.
[twoD addObject:[NSString stringWithFormat:#"%#%d",character,i]];
}
[self.words addObject:twoD];
}
// NSLog(#"->%#",self.words);
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
You probably should look at the UITableViewDataSource API documentation, specifically the -[UITableViewDataSource numberOfSectionsInTableView:] API.
You will also need to implement -[UITableViewDataSource tableView:numberOfRowsInSection:]. Both of these would need to be implemented in your class which implements the UITableViewDataSource protocol.
Please try to use this one....
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// ----- here you can return any value whatever you want. -----
return [array count];
}
You set totalSection and reload tableview
Before you set totalSection in [tableView reloadData];
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return totalSection;
}

Resources