I have a problem with my tableview. When I tap the add button in my tableview it sends me to a textview where I can write a text. Then when I save it, it moves back to the tableview and create a new row with the text from the textview as title. If the textview is empty it saves it with no text, and create a row without a title in the table view.
I want to delete all rows without a title in the tableview. I save the text to a NSMutableArray named "savedText".
Here is some code I have tried:
if ([savedText containsObject:#""]) {
savedText=nil;
}
and this:
if ([savedText isEqualToArray:#""]) {
savedText=nil;
}
Nothing happens when I use this code.
Conny, one of us doesn't understand or is missing something here. It's probably me, but just to try to help out anyway.
As I understand it you're storing all the entered strings into the array named savedText and want to go through all the items in the array to check if they're empty and remove them from the array if they are. Is that correct?
GuybrushTheepwood is right in his comment, you could simply check before storing it in the array. If it's empty, just don't store it and you'll never have to make the processor work to remove it.
However, if you still want an algorithm for removing empty strings you'll need to try something else. Your first if block will get rid of the entire array if it contains something that's empty, and your second one should be giving a warning that the parameter isn't an NSArray*. Maybe your looking for something like this?
for (NSString* string in savedText) {
if ([string isEqualToString:#""]) {
[savedText removeObject:string];
}
}
// Potentially more code here? Like reloading the tableview?
But that'll only remove it from the array, you'll need more than that to update the view.
Related
I'd like to get every data that is within all cells in one tableview which is quite a long list.
I'm looking for an approach on how to retrieve everything including those hidden in view, which I know the views are reused. I think some of you might have experienced this problem before, what are your approach on this?
I've tried
let cells = self.tableView.visibleCells
then looping into every cell and saving each data to an array but it is not effective in getting those that aren't part of the view or hidden. Is there a way to get over this?
In cellForRowAtIndexPath, YOU are telling the table what is in each cell. So why would you turn around and ask the table what's in each cell? If the user puts "Hello" in your first cell, then scrolls the table enough to push that first cell out of view, then when the user scrolls back to the top, YOU are the one telling it to put "Hello" back in that first cell. YOU own the data source, not the table.
You need a data source. That can be "empty" at first, maybe an array of empty strings if that's what you want (each index in the array could map to a table row for example). But then, as the user interacts with the text fields in the cells, you need to update that data source with the text they entered.
You should use that data source as your source for the cellForRowAtIndex method. That way you can handle populating the cells when they are requested by the table, and you also know all the data when the user is done.
Why not just update the model each time the user taps a key when editing a textfield? You could create a protocol for that cell subclass and make your view controller the delegate for each cell. As long as cells are guaranteed to stay on the screen while you're typing (you'll get some weird behaviors if not) the cell can send a message to the view controller or whatever you hook it up to telling it what new value to store. Then everything is already stored for you when you need the full list, and you don't have to interact with the tableview.
I use Parse for my backend data. Here, I saved an array of dictionaries to then query into my app and load into my tableview.
Here is my array saved in Parse:
When I query it in the viewDidLoad I set my empty array (self.dealListArray) equal to the array on Parse.
Here is what it looks like:
The first NSLog() outputs the entire array from Parse perfectly. But the second NSLog outside of the brackets output NULL.
The table view will not load up because my array is NULL. How can I keep the information from the query so that I can fill it with my table view?
Thank you, any help is much appreciated
You have no real guarantee when the block is called. So it's probably best that you wait until the data is loaded and meanwhile to have a UIActivityIndicatorView or something similar to indicate that something is being loaded.
Also, when the block does get called and you change the value of dealListArray you would have to reload the tableview's data.
like this...
self.dealListArray = [resMenu objectForKey#"Deals"];
NSLog(#"%#", self.dealListArray];
[self.tableView reloadData];
I am working on a UITableView inside a UIViewControlller and I would like to add a new cell to a row, but in a way that the only thing showing on the table would be the user defined name, but hidden data (such as a hyperlink) would be retained too. The purpose is to open such hyperlink when pressing the relative button.
I'm using
[dataSource addObject:UserDefineddName];
and this adds the name to the row, but how can I add the data too, without displaying it?
I have labels and a button set up in IB and I was thinking that perhaps the link could be assigned to a hidden label, but it's not clear to me how to do it.
Please advise!
Many thanks
Create a class for your data with two properties, name and url. Then simply fill your data source with instances of this class instead of simply strings. In your cellForRowAtIndexPath do something like this:
titleLabel.text = [dataSource objectAtIndex:indexPath.row].name;
In an app I previously had help with here, I parse an XML located online, show only 1 item from the XML the first day, and add an item each day after that the app gets opened. I would like to be able to add a 'Mark As Read' action to each cell's detail view. This way, the user could read it, mark it as read, and on subsequent loads, a checkmark would appear next to each item loaded.
Since the app downloads the XML each time, here is the idea I thought may work the best to do this. I was thinking of having an array stored to a NSUserDefault Key. This array would have a number added to it based off what row they selected. If they selected row 1 and marked it, the array would add the #1 to it. If they then selected row 3 and marked it, the array would have 1 & 3 in it.
Is this something doable, and then in cellForRowAtIndexPath, have it add a checkmark to each row # that is included in the array?
I have the idea in my head, and if this works, just need a little guidance for implementing it. Thanks
UPDATE
Here is what little I do have so far. In the applicationDidFinishLaunchingWithOptions, I check if the NSUserDefault key 'checkedrows' exists. If not, I create it with an object of an empty array.
if (![defaults objectForKey:#"checkedrows"]) {
[defaults setObject:#[] forKey:#"checkedrows"];
}
The two main questions I have is how to in the cellForRowAtIndexPath get the numbers that are in the array, and if any rows match them, add a checkmark to it. Example: There are 5 rows in the tableview, and the array returns 0, 2, 4. So, I want the TableView to add a checkmark to first third and fifth cells.
The other main question is how to go about adding a number into the array, without deleting any of the old numbers.
Absolutely!
You can use the setObject:forKey: method to write an NSArray object to NSUserDefaults, and objectForKey: to retrieve that array.
Now, simply listen out for
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
in your UITableViewDelegate, retrieve the array already existing in the NSUserDefaults, add the index that was just tapped (get this info. from the indexPath object) to the array, and simply write the array back to NSUserDefaults!
To add a checkmark to selected cells, use the following command inside thetableView:didSelectRowAtIndexPath: delegate method above:
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
I have a UiSearchBar implemented in my TableView, and I also have two NSArrays, one for title and one for description. When I search through the array of the titles, it returns the right search, but when I click on a row that the search came with, I get "row 0" if I click on the first row. My question is how to make a connection between the two arrays so that when the search rearranges the titles based on the user search, the description array corresponds to the same row the title is at.
Simply do not use two NSArrays, but just one with custom NSObject objects:
#interface SomeObject : NSObject {
NSString *_title;
NSString *_description;
}
- (BOOL)matchesKeywords:(NSString *)keywords;
#end
Then you have all your information stored in one class, the way Obj-C is meant to be. You can easily perform the search because the objects itself sort of knows whether it matches a given keyword, so when you'd like to change SomeObject you can easily manage those changes in the class itself.
I did merge the two arrays into one, but that made the tableviewcell load alot slower because the cell is holding both, the title and description
I had this problem once So for the quick fix I did this:
In the header:
BOOL usingFilterArray;
Where you switch between the complete dictionary and the filtered one simply set the above BOOL to NO and YES respectively.
then in didSelectRowAtIndexPath use "if" statement to check the sate of the usingFilterArray.
Rest should be pretty easy. (Let me know if you still need help)
Just one thing when you perform the search after the filter Dictionary is hydrated if you cancel the search you need to make sure to run this or your app is going to crash as the hydrated dictionary will not have any object in it. (I assumed you cleaned the filtered Dictionary)
- (void) searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
[self.tableView reloadData];
}
Mate this is just a quick fix.