UITableView Cell Dynamic Array not Loading - objective-c

Please help me figure out why the first line does not print the cell text while the second one does:
cell.textLabel.text = [NSString stringWithFormat:#"Hello %#", [swaItems objectAtIndex:indexPath.row]];
cell.textLabel.text = #"Hello";
No error is generated at any time.

Where you want the cell to initialize :
NSArray *swaItems = [[NSArray alloc] initWithObjects:#"1", #"2", #"3", nil];
NSString *string = [swaItems objectAtIndex:indexPath];
NSLog(#"%#", string);
cell.textLabel.text = string;
Works for me.

Related

NSString object not initialized properly

Can somebody tell me what I am doing wrong here. I am trying to initialize a string from a list of predefined names, but for some reason it doesn't work for NSString that is a class field:
MyController.h:
#interface MyController:UIViewController
{
NSString *text;
}
MyController.m:
-(void) viewDidLoad
{
[super viewDidLoad];
NSArray *list = [NSArray arrayWithObjects:#"A", #"B", #"C", nil];
NSString* temp= [list objectAtIndex:0]; // temp = "A"
text = [list objectAtIndex:0]; // Why text = nil ????
text = #"Hello"; // does not work either
}
Turns out the debugger is wrong in this case.
Try setting text equal to NSString *text = [NSString stringWithFormat:#"%#", [list objectAtIndex:0]];

Unknown recieiver 'indexPath'l did you mean NSIndexPath?

I'm parsing something right now and i want to display it in a cell i', using this code for my tableview but i also want it without Tablview
NSString *const JsonDataUrl = [NSString stringWithFormat: #"%#", appdownloadurl];
NSString *jsonString = [NSString
stringWithContentsOfURL:[NSURL URLWithString:JsonDataUrl]
encoding:NSUTF8StringEncoding
error:nil];
SBJSON *parser = [[SBJSON alloc] init];
NSDictionary *results = [parser objectWithString:jsonString error:nil];
parser = nil;
[self setTableData:[results objectForKey:#"resultCount"]];
This works great but when i place this line in viewdidload
NSDictionary *item = [tableData objectAtIndex:[indexPath row]];
NSLog(#"Apple: %#", [item objectForKey:#"price"]);
And error shows up on this line;
NSDictionary *item = [tableData objectAtIndex:[indexPath row]];
//Unknown recieiver 'indexPath'l did you mean NSIndexPath?
does anyone know how to fix this?
Do you declare any variable called indexPath in your viewDidLoad method? You can't just use the indexPath from your table view methods in viewDidLoad arbitrarily.
NSDictionary *item = [tableData objectAtIndex:[indexPath row]]; suggests you are trying to load from an NSArray, you probably want to try something like:
NSDictionary *item = [tableData objectForKey:#"Some Key"];

Objective-C, iOS, NSKeyedUnarchiver, only getting data in one cell

I tried for loops etc.. but nothing seem to work. I have a textfield and once I hit save, I puts the text in a table cell, If I do it again, the previous entry gets replaced. Basically, I can't seem to add another cell unless I manually addObject to the array. The data get pulled properly I used NSLog and the data saves as well.
I think the problem is here somewhere:
NSFileManager *filemgr;
NSString *docsDir;
NSArray *dirPaths;
filemgr = [NSFileManager defaultManager];
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
// Build the path to the data file
datafilePath = [[NSString alloc] initWithString: [docsDir
stringByAppendingPathComponent: #"data.archive"]];
tablesubtitles = [[NSMutableArray alloc]init];
tabledata = [[NSMutableArray alloc] init];
// Check if the file already exists
if ([filemgr fileExistsAtPath: datafilePath])
{
NSMutableArray *dataArray;
dataArray = [NSKeyedUnarchiver
unarchiveObjectWithFile: datafilePath];
titlestring = [dataArray objectAtIndex:0 ];
detailsstring = [dataArray objectAtIndex:1];
[tabledata addObject:titlestring];
[tablesubtitles addObject:detailsstring];
}
here is the other method for the actual table:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"homeworkcell"];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"homework"];
}
NSString *cellValue = [tabledata objectAtIndex:indexPath.row];
NSString *subtitle = [tablesubtitles objectAtIndex:indexPath.row];
cell.textLabel.text= cellValue;
cell.detailTextLabel.text= subtitle;
cell.textLabel.font = [UIFont systemFontOfSize:14.0];
cell.textLabel.backgroundColor = [ UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
// Configure the cell.
//-----------------------------------------START----------------------------Set image of cell----
cellImage = [UIImage imageNamed:#"checkboxblank.png"];
cell.imageView.image = cellImage;
//--------------------------------------------END---------------------------end set image of cell--
return cell;
}
here is where I'm saving the data:
NSMutableArray *contactArray;
contactArray = [[NSMutableArray alloc] init];
[contactArray addObject:titlefield.text];
[contactArray addObject:detailstextfield.text];
[contactArray addObject:date ];
[NSKeyedArchiver archiveRootObject:
contactArray toFile:datafilePath];
Do you recreate each time table data model?
tablesubtitles = [[NSMutableArray alloc]init];
tabledata = [[NSMutableArray alloc] init];
You should make class variables for this.

Objective C How to fill rangeOfString of a NSArray?

I wonder if it is possible to fill rangeOfString objects of a NSArray. Because I have a long list of objects for after rangeOfString:
NSArray biglist´s count is higher than list´s count.
I want to filter away the objects from the small list of the main list.
Please tell me if this is not clear.
My codes below:
NSArray *biglist = [[NSArray alloc] initWithArray:
[[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"mainlist" ofType:#"txt"]
encoding:NSUTF8StringEncoding error:NULL] componentsSeparatedByString:#"\n"]];
NSArray *list = [[NSArray alloc] initWithArray:
[[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"smalllist" ofType:#"txt"]
encoding:NSUTF8StringEncoding error:NULL] componentsSeparatedByString:#"\n"]];
for (NSString *listword in list);
NSMutableArray *wordlist = [[NSMutableArray alloc] init];
NSMutableArray *worindex = [[NSMutableArray alloc] init];
NSMutableIndexSet *mindexes = [[NSMutableIndexSet alloc] init];
NSMutableDictionary *mutdic = [[NSMutableDictionary alloc] init];
NSMutableArray *mutarray = [[NSMutableArray alloc] init];
for (NSString *s in mainlist)
{
NSRange ran = [s rangeOfString:listword];
if (ran.location !=NSNotFound)
{
//my codes here
}
}
EDIT:
I think I can solve this by writing
int i;
for (i = 0; i<[list count]; i++)
{
NSString *same = [list objectAtIndex:i];
NSLog (#"listword: %#", same);
}
But I am not sure where to place it, inside the for loop s in mainlist or outside.
EDIT: This for loop works inside the main for loop.
EDIT:
Tried these codes, but it doesnt work somehow..
NSArray *list = [[NSArray alloc] initWithArray:
[[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"small" ofType:#"txt"]
encoding:NSUTF8StringEncoding error:NULL] componentsSeparatedByString:#"\n"]];
NSArray *mainlist = [[NSArray alloc] initWithArray:
[[NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"mainlist" ofType:#"txt"]
encoding:NSUTF8StringEncoding error:NULL] componentsSeparatedByString:#"\n"]];
NSMutableArray *large = [NSMutableArray arrayWithArray:mainlist];
NSArray *newlarge;
for (NSString *listword in list)
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(SELF beginswith[c] %#)",listword];
newlarge = [large filteredArrayUsingPredicate:predicate];
}
NSLog (#"large: %#", newlarge);
NSLog (#"finished!");
"I want to filter away the objects from the small list of the main list."
If I understand correctly, you want to remove an array of items from another array. You don't want to do that much work and allocations inside an n^2 loop.
This removes an array of items from another array. Depending on how large your array is you may need to optimize further but this works:
NSArray *small = [NSArray arrayWithObjects:#"three", #"two", nil];
NSMutableArray *large = [NSMutableArray arrayWithObjects:#"one", #"two", #"three", #"four", nil];
[large removeObjectsInArray:small];
// print
for (NSString *current in large)
{
NSLog(#"item: %#", current);
}
This outputs:
2011-10-13 08:39:21.176 Craplet[5235:707] item: one
2011-10-13 08:39:21.178 Craplet[5235:707] item: four
I figured it out by myself and solved this :)
It works almost perfectly.
My codes:
NSArray *big = [[NSArray alloc] initWithObjects:#"hello ->mache", #"heisann hoppsann ->hiya", #"nei men ->da", #"however ->what", #"may ->april", #"mai ->maj", nil];
NSArray *small = [[NSArray alloc] initWithObjects: #"heisann ", #"nei men ", #"however ", #"mai", nil];
NSMutableArray *smallwithh = [[NSMutableArray alloc] init];
NSMutableIndexSet *mindexes = [[NSMutableIndexSet alloc] init];
for (NSString *same in small)
{
NSLog (#"listword: %#", same);
for (NSString *s in big)
{
NSRange ran = [s rangeOfString:same];
if (ran.location !=NSNotFound)
{
[smallwithh addObject:s];
NSUInteger ind = [big indexOfObject:s];
[mindexes addIndex:ind];
}
}
}
NSLog (#"smallwith: %#", smallwithh);
[smallwithh release];
NSMutableArray *newWords =[NSMutableArray arrayWithArray: big];
[newWords removeObjectsAtIndexes: mindexes];
[big release];
[small release];
NSLog (#"newWords: %#", newWords);

Objective C: combine two elements of a vector

I have this code:
cell.textLabel.text = [[appDelegate.client objectAtIndex:indexPath.row] objectAtIndex:0];
but I want write in the cell table view also the "objectAtIndex:1" then after
...objectAtIndex:0...????
what can I write?
NSString *a = [[appDelegate.client objectAtIndex:indexPath.row] objectAtIndex:0];
NSString *b = [[appDelegate.client objectAtIndex:indexPath.row] objectAtIndex:1];
cell.textLabel.text = [NSString stringWithFormat:#"%#%#", a, b];
See NSString Class Reference and Formatting String Objects.