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;
}
Related
I have two viewControllers, ViewController and TableViewController. the ViewController has a button, when pressed the TableViewController will start. the TableViewContoller contains 5 rows.
what I am trying to do is, when I press only the even rows I should go back to ViewController. To solve this problem, we can use unwindSegue, but I do not know how to have the segue respond to the even rows in the table.
please let me know how to make a row in the table linked to the segue
below is the code for TableViewController
code:
#import "TableViewController.h"
#interface TableViewController ()
#property NSArray *tableData;
#end
#implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.tableData = [NSArray arrayWithObjects:#"Android",
#"iOS",
#"swift",
#"objective-c",
#"openCV",nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section {
return [self.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 = [self.tableData objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:#"images.jpg"];
return cell;
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath {
NSLog(#"%li", indexPath.row);
NSLog(#"%#", [self.tableData objectAtIndex:indexPath.row]);
}
#end
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath {
if(indexPath.row % 2 == 0) {
// Even row...
}
}
I'm trying to make an editable section of rows using a UITableView
For some reason, my data does not load into the cell, and remains nil. I have been unable to figure out why, could someone give me some help ?
#interface exampleOrderCell : PSTableCell <UITableViewDataSource, UITableViewDelegate>
#property (nonatomic, strong) NSMutableArray *tableData;
#end
#implementation exampleOrderCell
//- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier specifier:(PSSpecifier *)specifier
- (instancetype)initWithStyle:(UITableViewStyle)style
{
return self;
}
- (void)viewDidLoad {
[self viewDidLoad];
self.tableData = [#[#"One",#"Two",#"Three",#"Four",#"Five"] mutableCopy];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ThisCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
cell.textLabel.text = self.tableData[indexPath.row];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
NSString *stringToMove = self.tableData[sourceIndexPath.row];
[self.tableData removeObjectAtIndex:sourceIndexPath.row];
[self.tableData insertObject:stringToMove atIndex:destinationIndexPath.row];
}
- (IBAction)startEditing:(id)sender {
self.editing = YES;
}
#end
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.
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];
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