Sorting UItableviewcell according to different conditions - iphone

I have customized uitableViewcell in which each cell show four data which are related to each other(ie object name,details, new price and old price). Data for each label Come from four different array.It is working fine. But now i want to sort table according to ascending price list or alphabetical order of object name on a button click. Problem is if I sort only the corresponding label change in each cell, others label remain same. i want to reorder each cell not individual label as four label are related to each other. Should I use NSDictionary for this or something else.Please guide me how to do it. Thanks.

This does not really have anything to do with the Table. You need to think data structures.
If I were you I will define a class with 4 attributes
Say
Class TableData
{
NSString * name;
NSString *details;
int newPrice;
int oldPrice;
}
Or something like that.
And now you can have ONE array with objects being instances of the above class.
Now you can sort the array by instance.name or instance.newPrice and relations will stay intact.
You then will just reload the tableView.

Related

Find indexPath of UITableViewCell by label

I have an UITableView which includes a few cells with tasks (label). I also have an array of type string which includes a few tasks which are displayed in the tableView. Now I want to check if an string from the array matches with an string from any cell. If it matches I wanna find out the indexPath of the matching cell.
You're thinking about this wrong. You need to slap yourself on the side of the head and say Model-View-Controller loudly several times.
Cells don't have strings. They are just views. In a certain sense, cells don't even exist! That's a very real thing: in, say, a 100-row table, only about 12 cells may exist at any one time. So there are no cells to look in.
The strings are in your model, not your view. Stop thinking about the table and think about where the information about the data that goes into your table is stored. That is where you want to look for the string. And when you find it, you will know the index path, because that's how your model is structured.

UITableView multiple section with multiple dictionaries

I am trying to work out on this scenario and just curious if it's possible to have multiple sections in one UITableView with with multiple NSDictionary contents? I have been trying to figure this and see if someone have had encountered this issue and how did s/he could get it resolved?
I have segregated count in numberOfRowsInSection method, but at cellForRowAtIndexPath I am having some issues with respect to how to segregate the sections with different dictionaries.
CellForRowAtIndexPath receives the indexPath parameter as an input. The indexPath.section attribute tells you what section of the table the cell is in, and indexPath.row tells you what row within that section. If you have a separate dictionary for each section, then you do a switch on indexPath.section, and get the data from the appropriate dictionary for that row.
If I understand correctly you are using 1 dictionary per section.
Dictionary are unordered, so you must keep in some way the key in a ordered fashion.
You can put your Dictionary in an array in the order you want to show them.
So that's for the section part of your NSIndexPath.
aDictionary = [arrayOfDic objectAtIndex:indexPath.section];
If you want to display the content of that dictionary in ascending order or your key you can do something like that :
anArray = [[aDictionary allKeys] sortedInSomeWayWithSomeSelector];
someObjt = [anArray objectAtIndex:indexPath.row];
Then you have the row for your NSIndexPath.
This is pretty generic, you will have to adapt it to your code.
And I would prefer to cache the orderedKey, because sorting at every row would be a waste of resources, but you got the general idea.

Marking an item as newly added in TableView

I have a UITableView where I can add a new item with a touch of a button. I would like to highlight this newly added item. Since UITableView is data-driven and cells are recycled, the only way I can think of so far to mark this newly added item is to add a BOOL flag in the data itself, then highlight the cell and negate the flag on first encounter.
Is there another way of doing this without having to contaminate the data source?
My first thoughts..
Create an Integer [myObjectArray count];
When you add the new object you can reload table
Create a new integer [myObjectArray count];
Compare the two integers when table reloads. If the 1st int is less than the new int
then you obviously have added another object.
When creating cell check if the cell indexpath.row is equal to the last object count in your array, if it is then you can fill the cell background with another color.
Hope that makes sense?
Assuming your data is ordered and indexable, use another NSArray (or NSMutableArray) of NSIndexPath to maintain a list of newly added data. If you only have one section, then you could substitute NSNumber for NSIndexPath and just record the rows.

Storing unique ID in UITableViewCell

I have a table where there will often be two cells with the same title. I'm zooming in on a map whenever a cell in the table is clicked, so using the title as a unique identifier is out of the question. I already have a unique identifier, but I need to find a way to store it in a UITableViewCell object.
I have been considering two options, both of which are poor, IMO.
1) store the unique ID as the text inside the text of the detailTextLabel property.
2) build a custom UITableViewCell class.
I'm new to objective C, and I would essentially like to know if theres a third option that isnt as inefficient as #1, but not as involved as #2.
Thanks
Use the cell's tag property (works only if your identifier is an integer).
That said, cells already have a unique identifier (albeit not necessarily constant) and that is their indexPath, i.e. their position in the table. You should generally not use the cells to store any data directly. Instead, store the data in a model object and maintain the connection between model data and view via the indexPath.
This is especially important when dealing with table views as cell views get reused or deallocated as soon as a cell is scrolled off the screen.

custom tablecell valueForSelectedRow problem

I have created a custom uitablecell which contains several values. When the user selects a row, I want to be able to select that row's content for one of the values in the custom uitablecell. If the cell looks like this:
ba_type varchar2(20) P
source varchar2(20) P
row_qual varhcar2(20)
If the user has selected the 'source varchar2(20) P' row, I want to be able to grab the value of 'source' out for my next query.
thx,
wes
Strictly speaking, you've created a tableview cell that displays several values. Sitting in memory somewhere behind that is an array that contains the actual data. That's how MVC architecture works.
Ideally, you have some data structure in memory that corresponds to the cells. If you had an NSMutableArray in memory, you could retrieve the value by doing
// assumes one section in the table view
NSString* sourceForNextQuery = [dataSourceUsedToPopulate objectAtIndex: indexPath.row];
inside didSelectRowAtIndex
If you insist on retrieving the text from the label in the table view cell, when you create the label that contains it, add a tag (mySourceLabel.tag = 999). Then find that tag again by using searching for the tag, the get the text (UILabel.text)
But really, you want to separate the presentation of the data from the underlying model, and have the controller contain the data.