I am getting data from json. It is working OK but the problem is that data is not showing in tableView.
I have checked the values that I assign to cell using NSLog but cell is not displaying any data.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int count = [surveyList count];
NSLog(#"These are rows %d",count);
return [surveyList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
ObjectData *data = [surveyList objectAtIndex:indexPath.row];
NSString *testingClient = data.survey_title;
NSLog(testingClient);
NSString *cellText = testingClient;
return cell;
}
Are you not missing this?
[[cell textLabel] setText:testingClient];
You aren't assigning any text to the cell without it.
You should assign that text to label of cell.
cell.textLabel.text = testingClient;
Related
I have an application which display large amount of values using UITableView and imported sqlite3 database. Now i face problem that i need to make sections for elements. It must be similar for default application "contacts" in iPhone, but i don't want for that section to sort elements only for first letter. Database contains data about food, so sections should be like "Bread", "Meat", "Fish", etc.
Please help, any advice will be appreciated!
Make a NSDictionary whose key is title to be displayed and value is NSArray which consist of all the objects that contain. Now do the following :
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[dict allkeys]count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection(NSInteger)section
{
//Number of rows it should expect should be based on the section
NSArray *array = [dict objectForKey:[[dict allKeys] objectAtIndex:section]];
return [array count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [[dict allKeys] objectAtIndex:section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
cell.text = [[dict objectForkey:[[dict allKeys] objectAtIndex:indexpath.section]] objectAtIndex:indexPath.row];
return cell;
}
I have an array with the following format:
[NSArray arrayWithObjects:
[NSArray arrayWithObjects:#"number",#"name",#"date",#"about",nil],
[NSArray arrayWithObjects:#"number",#"name",#"date",#"about",nil],
[NSArray arrayWithObjects:#"number",#"name",#"date",#"about",nil],
[NSArray arrayWithObjects:#"number",#"name",#"date",#"about",nil],
nil];
I want to structure this data to load into my tableView.
Each row of the tableview should correspond to each row of the array, then the title of each cell should correspond to the subarray objectAtIndex 2 for the name.
Suppose your array is named myData:
- (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 [myData 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...
NSArray *obj = [myData objectAtIndex:indexPath.row];
cell.textLabel.text = (NSString*)[obj objectAtIndex:1]; //note: 0=>number; 1=>name,..
return cell;
}
In favor of reusability, I would suggest to replace the subarrays with NSDictionaries so that you can get e.g. the name by calling [dict objectForKey:#"name"].
I have a UITableView whose datasource is a NSMutableArray. The array consists of a set of objects. All the cells are displayed in proper order.
Now I want to know how to display a last cell always with some text alone which is not there in the datasource array.
I hope i am clear enough :)
EDIT :----------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
// Return the number of rows in the section.
//+1 to add the last extra row
return [appDelegate.list count]+1;
}
// Customize the appearance of table view cells.
- (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];
}
NSUInteger index=[indexPath row];
if(index ==([appDelegate.list count]+1)) {
cell.textLabel.text = [NSString stringWithFormat:#"extra cell"];
}else{
Item *i = (Item *) [appDelegate.list objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:#"%# (%d %#)",i.iName, i.iQty,i.iUnit];
}
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
but i get the NSMutableArray out of bounds exception.
What could be wrong ?
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [your_array count] + 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
if (row == [your_array count])
{
cell.textLabel.text = [NSString stringWithFormat:#"Some text"];
}
else
{
cell.textLabel.text = your array object text;
}
return cell;
}
Your problem is the value you are checking against index. The list has count objects indexed 0 to count - 1. So you must check against count and not count + 1. As it is now, the request for countth row is entering the else section. There is no object at count in the list array. So you are getting the error. This is the modification.
if(index == [appDelegate.list count] ) {
cell.textLabel.text = [NSString stringWithFormat:#"extra cell"];
}else{
Item *i = (Item *) [appDelegate.list objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:#"%# (%d %#)",i.iName, i.iQty,i.iUnit];
}
in numberOfRowInSection return number of rows = your array count +1 then in Cell for cellForRowAtIndexPath check for indexPath.row if it is equla to your array count +1 then create cell that you want to add at last.
these is some code from my project..
for (index=0; index<[feed count]; index++) {
//NSLog(#"........%#",[feed objectAtIndex:2]);
NSString* forName=[feed objectAtIndex:index];
NSString *nameString = [forName valueForKey:#"name"];
NSLog(#"--------%#",nameString);
[nameArray addObject:nameString];
NSString *idString = [forName valueForKey:#"id"];
NSLog(#"--------%#",idString);
[idArray addObject:idString];
}
MyFriends *detailViewController = [[MyFriends alloc] initWithNibName:#"MyFriends" bundle:nil];
// ...
// Pass the selected object to the new view controller.
detailViewController.fbNames=nameArray;
detailViewController.fbIds=idArray;
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
My problem is in another controller when I am accessing array with names which is fbNames in another class it is showing me proper names,But when I am accessing fbIds which is the array with Ids ...they are showing me null..
how to correct this?
here...in this class ...fbNames are working fbIds are not..
- (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 [fbNames count];
}
// Customize the appearance of table view cells.
- (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...
cell.textLabel.text=[fbNames objectAtIndex:indexPath.row];
NSLog(#"%#",cell.textLabel.text);
return cell;
}
NSString* forName=[feed objectAtIndex:index];
NSString *nameString = [forName valueForKey:#"name"];
I don't think NSString has valueForKey:
I have a table with 50 rows. I want to select particular rows with checkmark accessory but when i select some rows and scroll down the table then i see pre checked rows. I know that table cell are reused but i want to emit this problem what can i do about this?
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// return [array count];
return 50;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text=[NSString stringWithFormat:#"%i",[indexPath row]];
return cell;
}
// Override to support row selection in the table view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
You have to set cell.accessoryType in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
The fact is that as you have a reuse identifier, when a new cell is requested, a cell with the identifier is copied. As you have set the checkmark in the latest cell, the checkmask is copied.
You have to store the state of each cell (ie in an array) to recreate the cell with the right value.
Something like that:
BOOL values[50]; //Not the best way, but easy for the example...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text=[NSString stringWithFormat:#"%i",[indexPath row]];
if (value[indexPath.row]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
values[indexPath.row] = !value[indexPath.row];
if (value[indexPath.row]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
You can do it with the help of array ,take one array and each array hold a dict do this condition in array
NSMutableArray *arr = [[NSMutableArray alloc]init];
for(int i=1; i<=50;i++)
{
NSmutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setobject:[nsstring stringwithformat:#"%d",i] valueForkey:#"ID"];
[arr addobject:dict];
[dict release];
}
now rows is [arr count];
and put this check in cell appearance
NSMutabledict *dict1 = [arr objectatindex:indexpath.row];
if([[dict objectforkey:"Check"]==nil])
{
[dict setobject:[nsstring stringwithformate:#"1"] valueforkey:#"Check"];
}else
{
if([[dict objectforkey:"Check"] isequaltostring:#"1"]
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
now put below code in didselect methood
nsmutabledict *dict1 = [arr objectatindex:indexpath.row];
if([[dict objectforkey:"Check"] isequaltostring:#"1"]
{
[dict setobject:[nsstring stringwithformate:#"2"] valueforkey:#"Check"];
}
else
{
[dict setobject:[nsstring stringwithformate:#"1"] valueforkey:#"Check"];
}