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.
Related
I am getting questions from a server using a php script. I am setting the number of cells according to the number of questions, but when I write to the cells, it only outputs 1 question. If I use a for loop, the cells are blank, but if I set the number it repeats the same question according to how many questions are in the database. Heres the code:
NSString *numOfQuestionsURL = #"http://**.***.**.**/count.php";
NSData *dataURLforSize = [NSData dataWithContentsOfURL:[NSURL URLWithString: numOfQuestionsURL]];
NSString *serverOutputforSize = [[NSString alloc] initWithData:dataURLforSize encoding:NSASCIIStringEncoding];
int numOfQuestions = [serverOutputforSize intValue];
for(int i = 0; i <= numOfQuestions; i++)
{
_hostStr = #"http://**.***.**.**/getQuestion.php?num=";
_appendString = [[NSNumber numberWithInt:i] stringValue];
_hostStr = [_hostStr stringByAppendingString: _appendString];
}
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString: _hostStr]];
NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding:NSASCIIStringEncoding];
result.textLabel.text = serverOutput;
_appendString = [[NSNumber numberWithInt:i] stringValue]; is where you tell the script what question you want to retrieve.
you need to use this code like this
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
NSString *numOfQuestionsURL = #"http://**.***.**.**/count.php";
NSData *dataURLforSize = [NSData dataWithContentsOfURL:[NSURL URLWithString: numOfQuestionsURL]];
NSString *serverOutputforSize = [[NSString alloc] initWithData:dataURLforSize encoding:NSASCIIStringEncoding];
return [serverOutputforSize intValue];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//make cell
_hostStr = #"http://**.***.**.**/getQuestion.php?num=";
_appendString = [[NSNumber numberWithInt:indexPath.row] stringValue];
_hostStr = [_hostStr stringByAppendingString: _appendString];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString: _hostStr]];
NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding:NSASCIIStringEncoding];
cell.textLabel.text = serverOutput;
return cell;
}
Also you can load data in background and maintain an array of question and populate in table.that makes your table smooth . currently your table behaves jerky.
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]];
With the following way...
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat:#"/lkj/"]];
NSString *fileName = [NSString stringWithFormat:#"/sandbox/2012_05_11.json"];
[[self restClient] loadFile:fileName intoPath:path];
NSString *fileContent = [[NSString alloc] initWithContentsOfFile:path];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *data = (NSDictionary *) [parser objectWithString:fileContent error:nil];
// getting the data from inside of "menu"
NSString *message = (NSString *) [data objectForKey:#"message"];
NSString *name = (NSString *) [data objectForKey:#"name"];
namegroup.text = [NSString stringWithFormat:#"%# %#",name, message];
...I am trying to parse a document I have previously made with other code...
{"message":["Untitled1a","Untitled2a","Untitled3a"],"name":["Untitled1b","Untitled2b","Untitled3b"]}
with the code above though, in name group.text, this appears...
(untitled, untitled, untitled) (untitled, untitled, untitled)
...but what I would like to do is to allocate many UITextFields, each of them in pairs, (2, 2, 2..), one field which displays the name and the other the message, so pair up 1a with 1b, 2a with 2b... obviously the fields won't be Untitled1a, but "how are you"...
But I can't seem to fix this issue!! Please help!!
You may try something like this:
NSArray *message = [data objectForKey:#"message"];
NSArray *name = [data objectForKey:#"name"];
NSDictionary* Dictionary = [NSDictionary dictionaryWithObjects:message forKeys:name];
for (NSString* Key in [Dictionary allKeys]){
NSLog(#"%# %#",Key,[Dictionary objectForKey:Key]);
}
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"];
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.