Filling up NSMutableArray - ios

I've got the code below to fill up an array of notifications. This array is needed to fill up a menu in my iOS app with notifications.
// extract specific value...
NSArray *switchValues = [res objectForKey:#"data"];
NSLog(#"%#", switchValues);
for (NSDictionary *switchValue in switchValues) {
NSString *text = [switchValue objectForKey:#"text"];
NSLog(#"%#", text);
[self.notificationsArray addObject:text];
}
Further down the line I then do this:
- (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];
}
NSMutableArray *titles = self.notificationsArray;
cell.textLabel.text = titles[indexPath.row];
return cell;
}
However, I keep getting an empty array. What am I doing wrong?

Why don't you do
[notificationsArray objectAtIndex:indexPath.row]
instead of
NSMutableArray *titles = self.notificationsArray;
cell.textLabel.text = titles[indexPath.row];
?
If you really dont want, at least do
NSMutableArray *titles = [NSMutableArray arrayWithArray:self.notificationsArray];
And in the first piece of code, what are the Console Logs? Are you sure you enter in the for loop?

Related

Inserting a new cell in between the cells in tableview

i here attached a snapshot of my sample output.now it has two cells that is according to arraycount which i used at number of rows in section method.i have calutalted the break time ,now i want to insert one more cells in between that two cells to dispay the break time . here is my code whic i did ....please help me to do this .thanks in advance
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"TableViewCell";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
NSLog(#"%#",self.arrData1);
NSDictionary *tempDictionary= [self.arrData1 objectAtIndex:indexPath.row];
//NSLog(#"%#",tempDictionary);
NSString *strFunctionCodeValue =[tempDictionary objectForKey:#"function_code"];
NSString *strStartTimeValue = [tempDictionary objectForKey:#"start_time"];
NSString *strEndTimeValue = [tempDictionary objectForKey:#"end_time"];
NSString *strTotalUnitsValue =[tempDictionary objectForKey:#"total_units"];
NSString *strTotalTimeValue = [tempDictionary objectForKey:#"total_time"];
//NSString *strBreakTimeValue = [tempDictionary objectForKey:#"break_time"];
cell.lblFunctionCodeValue.text = strFunctionCodeValue;
cell.lblStartTimeValue.text = strStartTimeValue;
cell.lblEndTimeValue.text = strEndTimeValue;
cell.lblTotalUnitsValue.text =strTotalUnitsValue;
cell.lblTotalTimeValue.text = strTotalTimeValue;
//cell.lblBreakTimeValue.text = strBreakTimeValue;
return cell;
}

Multiple UITableViews, One View, Two Filled, One Blank. What's the issue?

I have 3 tableViews in one view and for some reason two of them are showing data but one isn't, what's my issue?!!
I'm guessing the issue is in the cellForRowAtIndexPath declaration. Attached it is.
- (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];
}
if (tableView==_titleTV) {
NSDictionary *dictionaryTwo = [_titleArray objectAtIndex:indexPath.section];
NSArray *arrayTwo = [dictionaryTwo objectForKey:#"data"];
NSString *cellValueTwo = [arrayTwo objectAtIndex:indexPath.row];
cell.textLabel.text = cellValueTwo;
}
if (tableView==_cuisineTV) {
NSDictionary *dictionary = [_cuisineArray objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:#"data"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
}
else {
int storyIndex = [indexPath indexAtPosition: [indexPath length]-1];
cell.textLabel.text=[_favouritesArray objectAtIndex: storyIndex];
}
return cell;
}
What's the problem??
Thanks,
SebOH.
Try printing out the value of cellValueTwo to ensure its not a blank string:
NSLog(#"cellValueTwo value: %#",cellValueTwo);
Did you check to make sure the number of cells being created for _titleTV is correct with what you expect from - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section?

App crashes when button is pressed that points to table view

I've got a button pointing to a view and whenever I see press the button my app crashes. Can anyone tell me why? The code is the implementation file for the view that the button pushes to.
#implementation OpenShiftViewController
-(void)viewDidLoad
{
[super viewDidLoad];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
myDictionary = [defaults dictionaryRepresentation];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return myDictionary.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSArray * allKeys = [myDictionary allKeys];
cell.textLabel.text = [myDictionary objectForKey:[allKeys objectAtIndex:indexPath.row]];
cell.detailTextLabel.text = [myDictionary objectForKey:[allKeys objectAtIndex:indexPath.row]];
return cell;
}
#end
- (void)viewDidLoad
{
[super viewDidLoad];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
myDictionary = [defaults dictionaryRepresentation];
myMutableArray = [[NSMutableArray alloc]init];
[self storingObjectsFromDictionaryToArray];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)storingObjectsFromDictionaryToArray
{
NSArray *arr = [myDictionary allKeys];
[myMutableArray addObjectsFromArray:[myDictionary valueForKey:[arr objectAtIndex:0]]];
NSString *str = [NSString stringWithFormat:#"%#",[myDictionary valueForKey:[arr objectAtIndex:1]]];
[myMutableArray addObject:str];
str = [NSString stringWithFormat:#"%#",[myDictionary valueForKey:[arr objectAtIndex:2]]];
[myMutableArray addObject:str];
[myMutableArray addObjectsFromArray:[myDictionary valueForKey:[arr objectAtIndex:3]]];
[myMutableArray addObject:[myDictionary valueForKey:[arr objectAtIndex:4]]];
[myMutableArray addObjectsFromArray:[myDictionary valueForKey:[arr objectAtIndex:5]]];
[myMutableArray addObjectsFromArray:[myDictionary valueForKey:[arr objectAtIndex:6]]];
[myMutableArray addObject:[myDictionary valueForKey:[arr objectAtIndex:7]]];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return myMutableArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [myMutableArray objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [myMutableArray objectAtIndex:indexPath.row];
return cell;
}
The error is Thread 1: breakpoint 9.1
Ahm ... you might have a breakpoint activated ?
You are not getting an error - it is just cause of you having an activated breakpoint.
If you're using Xcode 4, then you can see 'Breakpoints' as seen below. Just click on that to disable breakpoints.
If you are using Xcode 5, then you have to click this Blue button to disable the breakpoint.
As AlexVogel pointed out in the comments, use init method to initialize the dictionary.
Alternatively, you can use lazy loading using property:
- (NSDictionary *)myLoadedDictionary{
if (!myDictionary) {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
myDictionary = [defaults dictionaryRepresentation];
}
return myDictionary;
}
Now, in your class, consume [self myLoadedDictionary] instead of myDictionary.
I am not on my machine, but I am pretty much sure that you can define even myDictionary as a getter so that you can use that same name in your code and not myLoadedDictionary.
Like everyone else I would like to point out that should post your error.
Now I also see one problem with cellAtindexPath logic. Your trying to set data on null cell, see [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; will only give you something back if you've already given it something.
You've two options for loading a cell, the new way or the old way, both are kinda of same.
Option one and I recommend this one:
in your viewDidLoad (or what not) register a class for a cell identifer:
[self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:#"cell"];
In CellForIndexPath to get the cell go:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
The forIndexPath is important here, using old api will return a nil cell.
Option 2:
Load a cell and if its nil, create a it.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"];
}

Display selective data in UITableView from an array

I Have an NSMutableArray which is being displayed as
(
13002,
My Drive,
13006,
Testing1,
13007,
Testing123
)
In my NSLog
I want to populate my UITableView with just the names (My Drive, Testing1 & Testing123) and want to use the ID's as the subtitle.
use cell for row at indexpath method and number of rows method as below
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return array.count/2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
int row = [indexPath row]*2;
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
}
cell.textLabel.text = [array objectAtIndex:row+1];
cell.detailTextLabel.text = [array objectAtIndex:row];
return cell;
}
your problem will solve...
NSmutableArray *element; //contains your array
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *cellIdentifier = #"MyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
element = [array objectAtIndex:indexPath.row];
cell.textLabel.text = element;
element = [array objectAtIndex:indexPath.row +1 ];
cell.subtitle.text = element;
Depending on your application, you might want to consider using a NSMutableDictionary instead of the array. This way, your data model will accurately represent the fact that those names are mapped to ids. You can use
NSArray *arrayOf Keys = [theDictionary allKeys];
to get an array of the keys in your dictionary, then do something like this:
cell.textLabel.text = [theDictionary objectForKey:[arrayOfKeys objectAtIndex:indexPath.row]];
cell.detailtextLabel.text = [arrayOfKeys objectAtIndex:indexPath.row];
You should also consider using the free Sensible TableView framework. You would just pass the array to the framework and it will automatically display it in the table view.

Displaying NSArray in UITableView (iOS)

I want to display in UITableView array like:
(
1,
{
date = 1351876762;
ncom = 0;
nid = 11739814;
"read_ncom" = 0;
text = "<div class=\"wikiText\"><!--4-->New note text </div>";
title = lol;
uid = 3795852;
}
)
Cells should have a label with "title" from this array.
How can I make that?
P.S.:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSString *textCell = [[array objectAtIndex:indexPath.row] objectForKey:#"title"];
cell.textLabel.text = textCell;
return cell;
}
returns signal SIGABRT.
Thanks.
[EDIT]
Your array seems to be weird. Try following and let me know the result.
NSString *textCell = [[array objectAtIndex:1] objectForKey:#"title"];
You should have cell with identifier "Cell" in your table (if you have storyboard) or create this cell manually
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [[array objectAtIndex:1] objectforkey:#"title"];
try the above code
Replace this :
NSString *textCell = [[[array objectAtIndex:indexPath.row] objectForKey:#"1"]objectForKey:#"title"];

Resources