Question on edit button on sqlite - iphone

I am trying to show minus symbol when I show data from an sqlite database, but if we show an edit button it shows a delete symbol. I am using navigation controller to show data. I want to show when the page loads, it must be with a "-" symbol without edit button? Does anyone have an example or tutorial?

If I understand you correctly, you want to delete the row immediately, without confirmation of tapping a delete button.
First: Users are used to having a delete button confirmation, and if you delete immediately, they may be surprised.
Second: If the issue is deleting many rows quickly, could you instead allow the user to select multiple rows and delete them all at once (like in the email app)
Third: A solution to your question:
Make a custom UITableViewCell and override this function
- (void)willTransitionToState:(UITableViewCellStateMask)state {
if (state && UITableViewCellStateShowingDeleteConfirmationMask) {
[delegate deleteCell:self];
}
}
You will need to also have a delegate property on the UITableViewCell subclass and set it to your view controller whenever you create a new cell. Then add this function in your view controller:
- (void) deleteCell:(UITableViewCell *)cell {
NSIndexPath *idx = [self.table indexPathForCell:cell];
if (idx) {
NSArray *arr = [NSArray arrayWithObject:idx];
//Delete from database here
if (/*delete success*/) {
[self.tableView deleteRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationFade];
}
}
}

Still not sure exactly what you're looking for, but if I understand better this time, you just want the - symbol to show up as soon as the view appears?
- (void) viewDidLoad {
[super viewDidLoad];
[self.tableView setEditing:YES];
}

Related

How to hide keyboard when UITextField is inside a TableView Cell

What I got is a TableView which is using Prototype Cells and an array feeding the dataSource - depending on the datasource, I'm using a certain Table View Cell identifier to build the cells. (Please correct me if I'm doing something wrong here - I am new to iOS dev and am looking for best practices wherever possible :)
Anyway, one of the Table View Cell I've created in Storyboard has a UITextField. I have it set up in Storyboard GUI that the Return Key is set to 'Done' and 'Auto-enable Return Key' is checked. I've then gone into my controller and used the following code:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
[textField resignFirstResponder];
NSLog(#"finished change...");
}
I've also got a 'Editing Did End' event linked to my controller which is coded to output to NSLog.
The Log outputs the text placed in textFieldDidEndEditing and my event function when I hit Return or Done. However, when I select a table cell, or start moving the slider nothing happens - meaning the keyboard is still on the screen.
Is there a way to resolve this?
Also, whilst I've been working on this Prototype Cells, it seems to be rather laborious for what I need. Basically all I require is a basic table to allow the user to edit some settings, so there would be:
2 textfields that can be edited,
1 slider,
2 switches
I placed all of this in a Static UITableView however it did not appear - this is due to a restriction on this type of table requiring a UITableViewController. Is there a way around this?
Sorry for the long post - I'm still getting my head around iOS dev but I am enjoying it so far :)
Any help would be greatly appreciated.
I usually keep an ivar around to keep track. It lets me conveniently dismiss the keyboard based on all kinds of events:
#interface ViewController () {
UITextField *_textFieldBeingEdited;
}
#end
- (void)textFieldDidBeginEditing:(UITextField *)textField {
_textFieldBeingEdited = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
// no need to resignFirstResponder here.
_textFieldBeingEdited = nil;
}
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (_textFieldBeingEdited) {
[_textFieldBeingEdited resignFirstResponder];
}
...
}

How to create dropdownlist without using UIPIckerviewcontroller in IPhone?

I want to dropdownlist like listview at when i click the dropdown button and make the list have some contents. then any content i select it will be text for the label any one help me. Thanks Lot.
You can use pop-over to display list.In pop-over you can create tableview to display list of items and when user selects any option, didSelectRowAtIndexPath will be called, from this method you can send the selected value and display in label.
Code in mainviewcontroller, where you want to display drop down.
if (m_OptionController !=nil)
{
[m_OptionController release]; m_OptionController = nil;
}
m_OptionController=[[OptionViewController alloc]init];
[m_OptionController setTarget:self andSelector:#selector(displaySelectedOption:)];
if(m_pPopOverController)
{
[m_pPopOverController dismissPopoverAnimated:YES];
[m_pPopOverController release];
m_pPopOverController=nil;
}
m_pPopOverController=[[UIPopoverController alloc]initWithContentViewController:m_OptionController];
[m_pPopOverController setPopoverContentSize:CGSizeMake(thePopOverFrame.size.width, thePopOverFrame.size.height) animated:NO];
[m_pPopOverController presentPopoverFromRect:CGRectMake(theButton.frame.origin.x,0,40,40) inView:self
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
OptionViewController is a UIViewController which will contain UITableView.Populate UITableView with data(list of options).
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([m_Target respondsToSelector:m_Selector]) {
[m_Target performSelector:m_Selector withObject:nil];
}
}
Do not forget to set target by calling this method, so when user selects any option, corresponding method in mainviewcontroller is called where you want selected value.
- (void)setTarget:(id)inTarget andSelector:(SEL)inSelector
{
m_Target = inTarget;
m_Selector = inSelector;
}

Help With NSNotifcation and Asynchronous Downloading

I am sending a notification from one view to another view. My problem is that the notification in the view that I am calling in my cellForRowAtIndexPath method is only getting sent when the tableview is scrolling. How can I stop this and make it send the notification once the images have downloaded? Here is my code: https://gist.github.com/756302
Thanks
MKDev
as far as I understand your code, the message will trigger the reload of the whole table. That should lead to a refresh of the cells.
Thus, you'll need to check in line 76, if the cell is being drawn because a reload was triggered from the finish-message (and the image is now ready to display) or if you need to start the asynchronous download of the image.
The first thing which comes into my mind to check this is to set a property in reloadTableView:
- (void)reloadTableView
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"aaa"];
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"name" object:nil];
NSLog(#"removeobserver");
loadImageFinished = YES;
// if your table has several sections you'll need to adopt the section number
NSIndexSet *indices = [[NSIndexSet alloc] initWithIndex:0];
[self.tableView reloadSections:indices withRowAnimation:UITableViewRowAnimationFade];
[indices release];
}
and then to add in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
if (loadImageFinished) {
...
} else {
[asyncImage loadImageFromURL:[NSURL URLWithString:pathImage]];
}
...
}
Note that there could be other reasons why the table is being reloaded - the view could have been disappeared or unloaded and you might not wish to trigger your asynnchronous loading several times.
Your code should work right, when the connectionDidFinishLoading, you call the NSNotificationCenter to send the notification, there is no post method in cellForRowAtIndexPath

Is there any exit editing mode for UITableView?

The flow is something like that... The user see a UITableView, when the user click the "edit", the user can delete the UITableView's cell, after that, he /she click done to confirm. Which method is called when the user click the done key? Thank you.
On UITableViewController it's setEditing: with NO as argument.
By overriding the UITableViewController's setEditing:animated: method, you'll get the message you are looking for.
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
if (editing) {
// User tapped the edit button
} else {
// User tapped the done button
}
}

What's with [UITableView reloadData]?

I have an application that has a UITableView. This UITableView is populated by an NSMutableArray being held (as a property) in the appDelegate. You can think of this as an email window. It lists messages in a subclassed UITableViewCell. When a new message appears, I have all the code done which downloads the message, adds the data to the appDelegate's NSMutableArray which holds all of the messages. This code is working fine.
Now, once the new message is downloaded and added to the array, I am trying to update my UITableView using the following code, however - the UITableView's delegate functions do not get called.
The odd thing is when I scroll my UITableView up and down, the delegate methods finally get called and my section headers DO change (they show the message count for that section). Shoudn't they update in real-time and not wait for my scrolling to trigger the refresh? Also, the new cell is never added in the section!!
Please Help!!
APPDELEGATE CODE:
[self refreshMessagesDisplay]; //This is a call placed in the msg download method
-(void)refreshMessagesDisplay{
[self performSelectorOnMainThread:#selector(performMessageDisplay) withObject:nil waitUntilDone:NO];
}
-(void)performMessageDisplay{
[myMessagesView refresh];
}
UITableViewController Code:
-(void) refresh{
iPhone_PNPAppDelegate *mainDelegate = (iPhone_PNPAppDelegate *)[[UIApplication sharedApplication] delegate];
//self.messages is copied from appDelegate to get (old and) new messages.
self.messages=mainDelegate.messages;
//Some array manipulation takes place here.
[theTable reloadData];
[theTable setNeedsLayout]; //added out of desperation
[theTable setNeedsDisplay]; //added out of desperation
}
As a sanity check, have you verified that theTable is not nil at that point?
You could try putting a delay on the reloadData call - I had a similar problem when I was trying to get my tableview to update when reordering cells, except that the app crashed if I called reloadData during it.
So something like this might be worth a try:
Refresh method:
- (void)refreshDisplay:(UITableView *)tableView {
[tableView reloadData];
}
and then call it with (say) a 0.5 second delay:
[self performSelector:(#selector(refreshDisplay:)) withObject:(tableView) afterDelay:0.5];
Hope it works...
If you call reloadData from within a dispatched method, make sure to execute it on the main queue.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0), ^(void) {
// hard work/updating here
// when finished ...
dispatch_async(dispatch_get_main_queue(), ^(void) {
[self.myTableView reloadData];
});
});
..same in method form:
-(void)updateDataInBackground {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0), ^(void) {
// hard work/updating here
// when finished ...
[self reloadTable];
});
}
-(void)reloadTable {
dispatch_async(dispatch_get_main_queue(), ^(void) {
[myTableView reloadData];
});
}
Have you tried setting a breakpoint in your refresh method just to be sure your messages array has the correct content before calling reloadData?