NSTableView rows using NSArray - iphone

I need to Write 4 sections in a table where two of them have only a single row.
So is there any other way i could do it instead of using NSArray for just a single row?

You can create two dimensional array using NSMutablearray
You can access them by using
[[array objectAtIndex: ] objectAtIndex: ]
Hope it helps.....

Dont know from where you are getting the data for your 2 single rows but you could make a switch(indexPath.section)/case or an if(indexpath.section == yoursectionnumber) logic in your cellForRowAtIndexPart delegate method and there you can assign a value to your cell.
As DShah wrote, you can use variables and such for this so you could for example do a cell.textLabel.text = myString;
Where myString is an NSString created by you.

Related

How to Display this kind of data into UITableview

I am having NSArray in which it is having following data, i am using AFHTTPRequestOperation which give me result and after that i am doing
[ListOfName addObject:[responseData valueForKey:name]]; and getting following result and that result i want to display in tableview but can understand how to do it becuause i an new to iphone
(
(
"Richard Conover",
"Richard Conover",
"Kaitlyn Matheson",
"Andrea Wannemaker",
"Andrea Wannemaker",
test,
james,
test,
gaurav,
sdfsdfs
)
)
if i do NSArray.count it will return only 1 so how to print it separately in tableview
What you want to do is set the TableView datasource to be the first object in that Array (which is another Array). Something like this:
NSArray *myTableViewDataSourceArray = [myOriginalArray objectAtIndex:0];
Then use myTableViewDataSourceArray for the datasource methods of the TableView.
As per your structure, you having array with objects which also array.
That means,[ListOfName addObject:[responseData valueForKey:name]]; will add array object. You try to load this array of data into your table view. So you can try this in your tableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView
{
return [[ListOfName objectAtIndex:0] count];
}
It's a Nested Array (means Array inside Array).
Howmany Nested Arrays you have ?
If it's 1 then you can use :
NSArray *finalArray = [[NSArray alloc] initWithArray:[yourArray objectAtIndex:0]];
then you can use finalArray to populate the UITableView.

Iphone : How to get array value in didSelectRowAtIndexPath?

I am using google Api for custom search and have done the json Parsing. I succesfully retreived the values from JSON and store that data in Dictionary.
NSArray *latestdata = [(NSDictionary*)[responseString JSONValue] objectForKey:#"items"];
Now I have two array in it link and title
titleArray = [latestdata valueForKey:#"title"];
linkArray = [latestdata valueForKey:#"link"];
Now i use the title array to store the title at CellForRowAtIndexpath and i want to use the link for that specified or selected index at DidSelectIndexPathAtRow.
I have already filled the cell by the title array as below
cell.textLabel.text = [title objectAtIndex:indexPath.row];
But now i dont when i select the row at table how can i get the link for that using the link array at didSelectRowAtIndexPath method.
What could be the solution for this.
Try following code at didSelectRowAtIndexPath
NSLog("Value : %#", [title objectAtIndex:indexPath.row]);
I hope title is global array.
You would use the same paradigm you set the title with. You have indexPath.row available to you in didSelectRowAtIndexPath.
My worry with this is ensuring the arrays are in sync. Would you no be better served using a Map for the data? If you are more comfortable, you can store the titles in an array, because parsing that array to build your table is really straight forward. But how do you know the element in each spot in the title array matches its corresponding member of the link array? Are you sure [title objectAtIndex: 1] matches the desired link in [link objectAtIndex: 1]? Key-value coding will harden this and give you an easier to maintain solution in my opinion.
Good Luck.

How to parse XML array in iPhone?

I am doing parsing, in my parsearray having only two data number of alerts and number of events these are fine. When we are finding the value like
NSString *alertcount = [[xmlparseArray objectAtIndex:indexPath.row ]objectForKey:#"alerts"];
and assigning these string value into label
mylabel.text = [NSString stringWithFormat:#"%#", alertcount];
same for Events.
then it becomes crash. It says:
index 0 beyond out of empty Array
And these parsing I am using in #RootViewController:
UITableViewController {
}
not using custom cell. In this rootviewcontroller class we are using 7 row(these are constant) and I want to assign at row number 5 the value of alert (i.e. mylabel.text = [NSString stringWithFormat:#"%#", alertcount]) at the right position of cell number 5 same is cell number 6.
Your xmlparseArray might be empty. Check if you have any elements in the array. Pls show the code where you populate the array so that we can have a better idea of where you are going wrong

What's the best way to return different cells depending on position in a UITableView

I have a grouped UITableView that has 3 sections and a number of cells in each section that each need to be a different custom cell, with different display requirements.
I currently have a large if statement in cellForRowAtIndexPath, with each branch instantiating and returning the appropriate custom cell based on interrogating the indexPath's section and row properties. e.g.
if (indexPath.section == 0 && indexPath.row == 0) {
// instantiate and return cell A
} else if (indexPath.section == 1 && indexPath.row == 2) {
// instantiate and return cell B
} //etc etc
What is best practice for this scenario? A large if statement does the job, but is it the best implementation?
By far the best way to do this is described in this post from Cocoa With Love. Basically it's a custom controller for each cell type but it ends up being really simple to implement.
One method I've used once or twice is a nested series of NSArray objects that I initialize in viewDidLoad:
// sec<n>Row<n>Cell are IB Outlets to UITableViewCell objects defined in your .xib
NSArray *firstSectionRows = [[NSArray alloc] initWithObjects: sec1row1Cell, sec1row2cell, nil];
NSArray *secondSectionRows = [[NSArray alloc] initWithObjects: sec2row1Cell, sec2row2cell, nil];
// cellTree is an instance variable
cellTree = [[NSArray alloc] initWithObjects: firstSectionRows, secondSectionRows, nil];
[firstSectionRows release];
[secondSectionRows release];
Then in your tableView:cellForRowAtIndexPath: method would look like this:
...
return [[cellTree objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
...
This assumes that each of your cells is unique, in which case you don't even need to try and dequeue reusable cells. If you have more than one of the same type of cell in any section, you'll have to create/dequeue them appropriately and assign them a unique cell-type identifier.
If you have a large or complicated structure you might be able to do your array setup in a .plist file and use NSArray's initWithContentsOfFile: method to read it in. However you will have to do some kind of KVC magic to get cell objects from the strings in your array:
return [self valueForKey:[[cellTree objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]];
On complex table views I've considered splitting up the UITableViewDataSource routines into sub-delegates, one delegate for each section. So for 3 sections, I would create 3 separate objects each one responsible for providing the UITableViewDataSource methods for that section of the table. Then the UITableView's delegate just dispatches to one of the appropriate sub-delegates depending on which section the request is for.
I think this is pretty much common practice. Just be sure that you specify a unique cell identifier for those different cells. So that caching of the cells works properly.

iPhone - Sort UITableView by Array Index

I have an UITableView set up with a NSArray with 10 indexes. Right now, the first cell on the uitableview is the first index, the second cell is the second index, and so on so forth. Is there any way to make it so that the first cell displays the latest index? Maybe through some code in the uitableview delegate because I am adding data to the NSArray. What that means is that there aren't 10 indexes right off the bat.
If anyone as an answer, help is much appreciated.
Each time that you get a new item of data, you add it to the start of your array, not to the end. Then just call [self.tableView reloadData] and it should just work.
You can use insertObject:atIndex: to add to the start of the array:
[myArray insertObject:newData atIndex:0];
(see here for docs)
Somewhere in your code you're probably doing something like this (where items is your NSArray object):
cell.textLabel.text = [items objectAtIndex:indexPath.row];
Instead, do:
cell.textLabel.text = [items objectAtIndex:([items count] - 1) - indexPath.row];