Insert Cells and Sections from Table Views - objective-c

Help me ! I can not able to insert cells in section tableviews !
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
NSInteger result = 0;
result = [[_dictionoryOfDay allKeys] count];
return result;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSInteger result = 0;
NSString *sectionNameInDictionary = [[_dictionoryOfDay allKeys]
objectAtIndex:section];
NSArray *sectionArray = [_dictionoryOfDay objectForKey:
sectionNameInDictionary];
result = [sectionArray count];
return result;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UICustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cellMain" forIndexPath:indexPath];
[cell.btnAddMore addTarget:self action:#selector(actionAddMore:)forControlEvents:UIControlEventTouchUpInside];
cell.btnAddMore.tag = indexPath.section;
return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
TableHeaderAvailabel *header;
if(!header){
header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:#"TableHeaderAvailabel"];
}
NSString *result = nil;
result = [[self.dictionoryOfDay allKeys] objectAtIndex:section];
header.lbTitle.text = result;
return header;
}
-(void)actionAddMore:(UIButton *)sender{
}

My guess is that you want to add a row to your section when you tap on a cell's btnAddMore. If that's the case, then the implementation of actionAddMore() should look like
NSString *sectionNameInDictionary = [[_dictionoryOfDay allKeys]
objectAtIndex:section];
NSArray *sectionArray = [_dictionoryOfDay objectForKey:
sectionNameInDictionary];
// Create whatever you use to populate rows
[sectionArray addObject: <your new object>];
_dictionaryOfDay[sectionNameInDictionary] = sectionArray;
[self.tableView reloadData]; // Or just load the new cell, but this is easier

Related

Sort cell in tableview by title (date)

How to sort the cell in the tableview by title (title is date in dd/mm/yyyy format)?
My code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#"total item in table %lu",(unsigned long)self.items.count);
return self.items.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"
forIndexPath:indexPath];
[self configureCell:cell atIndex:indexPath];
return cell;
}
- (void)configureCell:(UITableViewCell*)cell atIndex:(NSIndexPath*)indexPath {
// Get current Item
Item *item = self.items[indexPath.row];
cell.textLabel.text = item.name; // HERE IS DATE in dd/mm/yyyy format
cell.detailTextLabel.text = item.itemDetails.note;
/*
NSArray *productsOrdered = nil;
productsOrdered = [self.items sortedArrayUsingDescriptors:#[[NSSortDescriptor sortDescriptorWithKey:#"name" ascending:YES]]];
*/
}
}
I get this:
It's not sorted correctly.
I need the newest date up and older dates down in the list.
Sort self.items however you'd like to before calling [self.tableView reloadData];

Objective-C array issues

I have this NSArray and each item returns a POIndex and PONumber and I am putting each item of the array inside another Array and the other array is returning like this:
POIndex
PONumber
POIndex
PONumber
Here is my code:
- (void)GetRequest
{
NSArray *tableData = [dataSource.areaData GetPurchaseOrderItems:[NSString stringWithFormat:#"%#%#",areaPickerSelectionString,unitPickerSelectionString]];
if(!self.objects){
self.objects = [[NSMutableArray alloc]init];
}
for(int i = 0; i < [tableData count]; i++){
[self.objects addObjectsFromArray:[tableData objectAtIndex:i]];
}
[self.tableView reloadData];
}
Here is a screenshot of what tableData is returning:
What I am looking to do is have have the PONumber as the key and POIndex as the display value, how would I do this?
I have tried the following:
NSMutableDictionary *subDict=[[NSMutableDictionary alloc]init];
for (NSDictionary *dict in tableData) {
[subDict setValue:[dict objectForKey:#"POIndex"] forKey:[dict objectForKey:#"PONumber"]];
[self.objects addObject:subDict];
}
but this displays like so:
{
{
{
{
Here is what is displaying it:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSString *object = self.objects[indexPath.row];
cell.textLabel.text = [object description];
return cell;
}
Your tableData array contains NSDictionary at each index. So you can use the same array in your cellForRowIndexPath method like as follows,
#property(nonatomic, retain) NSArray * tableData;
- (void)GetRequest
{
self.tableData = [dataSource.areaData GetPurchaseOrderItems:[NSString stringWithFormat:#"%#%#",areaPickerSelectionString,unitPickerSelectionString]];
[self.yourTableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSString *strPOIndex = [self.tableData[indexPath.row] valueForKey:#"POIndex"];
cell.textLabel.text = strPOIndex;
return cell;
}

UITableView scroll is never end after search

I have UITableView to show whole bunch of words from array.
I also provide search bar on top of table view so when people type a word in textbox search then my code start filtering data from array.
My result is good but my scroll in tableview is never end even though there is only one row.
My tableview result can scroll down and down without no row.
I don't know what going on with my table view.
My code that I have implement.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if(self.tableView == tableView){
return [self.alphabet count];
}else{
return 1;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.tableView == tableView) {
return [[self.vocabularyInfo objectAtIndex:section] count];
}else{
return [self.filteredVocabs count];
}
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [self.alphabet objectAtIndex:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
if(self.tableView == tableView){
return self.alphabet;
}else{
return nil;
}
}
// Display cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"CommentTableCell";
//-- try to get a reusable cell --
CommentTableCell *cell = (CommentTableCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//-- create new cell if no reusable cell is available --
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:cellIdentifier owner:self options:nil];
cell = [nib objectAtIndex:0];
}
VocabularyController *vc;
// Display word from database else display vocabulary when searching
if (tableView != self.searchDisplayController.searchResultsTableView) {
vc = [self.vocabularyInfo[indexPath.section] objectAtIndex:indexPath.row];
}else{
vc = [self.filteredVocabs objectAtIndex:indexPath.row];
}
cell.nameLabel.text = vc.korean;
return cell;
}
// My method that I used for filtering my array to display the result
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
if (searchString.length > 0) {
NSArray *vocabToSearch = self.vocabularyInfo;
if (self.currentSearchString.length > 0 && [searchString rangeOfString:self.currentSearchString].location == 0) {
vocabToSearch = self.filteredVocabs;
}
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"korean CONTAINS[cd] %#", searchString];
self.filteredVocabs = [vocabToSearch filteredArrayUsingPredicate:predicate];
} else {
self.filteredVocabs = self.vocabularyInfo;
}
self.currentSearchString = searchString;
return YES;
}
Please help me!!
Check your tableview's datasource delegate function.
Maybe more detail about your implementation is helpful.
Please check the value you are returning for the number of rows in the following method:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

How to add multiple Objects to Array - Objective C / iOS

I'm new to programming in Objective C
Here is my Dilemma: I'm pulling in a JSON file from the web and I'm able to display one of the elements (currDate) to my tableView but now I want to display more. From the code below I would I get it to display both currDate and prevDate
The logic needs to be changed here:
for (NSDictionary *diction in arrayOfEntry) {
NSString *currDate = [diction objectForKey:#"Current Date"];
a = currDate;
NSString *prevDate = [diction objectForKey:#"Previous Date"];
b = prevDate;
[array addObject:a];
}
[[self myTableView]reloadData];
I'm not sure if I need to change anything here but I'm attaching it to show how I'm displaying the array to my viewTable:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [array count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [array objectAtIndex:indexPath.row];
//cell.textLabel.text = [array objectsAtIndexes:indexPath.row];
return cell;
}
just add another line:
[array addObject:a];
[array addObject:b];
make arrayOfEntry global. in .h file
NSArray *arrayOfEntry;
In numberOfRowsInSection
[arrayOfEntry count]
In tableView: cellForRowAtIndexPath
NSString *currDate = [[arrayOfEntry objectAtIndex:indexPath.row] objectForKey:#"Current Date"]
NSString *prevDate = [[arrayOfEntry objectAtIndex:indexPath.row] objectForKey:#"Previous Date"]
cell.textLabel.text = [NSString stringWithFormat:#"%# %#", currDate, prevDate];
To add multiple elements to an Array in objective c you need to use:
NSMutableArray *newArr = [NSMutableArray new];
Then:
[newArr addObject:dict1];
If you want, you can then set your NSArray you were using to the NSMutuableArray after the addition of objects is complete.

How to load a tableView with an array of dictionaries?

I want to load a table view with an array which itself has multiple dictionaries. Each of these dictionaries has items which I need to put into a row, one dictionary per row. All of these dictionaries are stored in an array.
How should I perform this task?
Thanks,
See the code below, projects is my array.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return self.projects.count;
}
- (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];
}
// Configure the cell.
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
cell.textLabel.text = [self.projects objectAtIndex:indexPath.row];
}
You can do this with pretty much what you have. You'll just need to change your configureCell:atIndexPath: method to look something like this:
cell.textLabel.text = [[self.projects objectAtIndex:indexPath.row] objectForKey:#"some_key"];
Just change some_key to the key in your NSDictionary that you want to display in your UITableViewCell.
You can also change your UITableViewCell style to UITableViewCellStyleSubtitle and then you can add another key from your NSDictionary to cell.detailTextLabel.
In addition to edc1591,
In case if the dictionaries in the array have different keys then
if([[self.projects objectAtIndex:indexPath.row] count]==1)
{
id key= [[[self.projects objectAtIndex:indexPath.row] allKeys] objectAtIndex:0];
cell.textLabel.text = [[self.projects objectAtIndex:indexPath.row] objectForKey:key];
}