Change Static Table Views like contacts app on Iphone - iphone

I know this question has been asked before, but no one has really answered it.
I am trying to make an app with a static table view and a Done/Edit button in the top-right corner. I need to be able to hide a cell when it is in one state, and display that cell when it is in the other state. Also I need to be able to add cells when the user selects something. I all ready have the bool in place to detect the change of the Done / Edit button.
So basically my question would be: how do you go about making the table view display the cell when the user presses the button, and hide it when the user presses it again.
And how to add the static cells through the code.
Thanks!

I haven't really looked at the static table stuff in iOS 5 because I believe that requires storyboards, which I don't use.
This can be easily accomplished with a classical grouped UITableView, however. In your UITableViewDataSource methods merely return different results based on the editing state. If you are using the literal editing mode of the UITableView you could do something like the following (warning: typed in browser):
– (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if ([tableView isEditing]) {
// Return number of sections when editing
}
else {
// Return number of sections when not editing
}
}
– (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
if ([tableView isEditing]) {
// Return number of rows in section when editing
}
else {
// Return number of rows in section when not editing
}
}
// etc.
I don't recall whether the -setEditing: transition handles animation for you, but if it doesn't then you will want to use -insertRowsAtIndexPaths:withRowAnimation: and related methods to notify the table view that the number of rows, sections, etc. has changed and that it should animate to the new layout. Finally, if you are adding/removing multiple rows/sections, as always you probably want to wrap your work in a -beginUpdates/-endUpdates pair so that the animations are all coalesced.

Related

Keeping one section open at a time in UITableView based accordion view based on apple sample code TVAnimationsGestures

I'm building an app with a UITableView-based accordion view. This code is based on Apple sample code which can be found here or here.
In the sample code, the problem I'm trying to fix is there, so I know that it's not something that I introduced.
If you open multiple sections without closing the previous one explicitly, multiple sections can be open at the same time. This can lead to a crash, which can be replicated by hitting buttons 1, 2, 3, 1, 1, 3.
In my app, I am trying to make the previous section close and the button in the header go to an unselected state when a new section is opened, so that you can only ever have one section opened and one section header button selected at a time. If anyone has any experience with this sample code or this use of a tableview, I'd love to correct this, especially since it's a problem inherent in Apple's own code
The property APLSectionHeaderView* headerView of APLSectionInfo is never set.
So set sectionInfo.headerView = sectionHeaderView in tableView delegate method.
-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section {
APLSectionHeaderView *sectionHeaderView = [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:SectionHeaderViewIdentifier];
APLSectionInfo *sectionInfo = (self.sectionInfoArray)[section];
sectionHeaderView.titleLabel.text = sectionInfo.play.name;
sectionHeaderView.section = section;
sectionHeaderView.delegate = self;
sectionInfo.headerView = sectionHeaderView;
return sectionHeaderView;
}

Edit a UIViewController

I can't seem to find any information on what should be a simple issue. I have a table which contains a series of cells. When you tap on the cell it shows the information of that object in detail. I know it is possible to have a button which places all of the data on the screen in EDIT mode. Any tutorials or advice as how to do this (properly/ with best practices)?
Just to be clear this is for iPhone/ Objective-C/ Cocoa.
Thanks,
EDIT 1
Sorry. I know how to put the button there. But how do make the labels editable?
You are confusing two states:
Putting the tableView into editing mode - this is for deleting, or moving cells around in the table, regardless of the cell content. This is controlled by the UITableView.editing property.
and
Putting the tableViewCell into some sort of editing state. There is no official editing state for the cell (i.e. there is no single flag to set to make all UILabels in a cell into editable textFields.) You need to implement all of this logic yourself. If you're using .xibs, a good practice here is to have a different .xib for your cell's editing mode.
You cannot have editable labels. However, you can replace the label with a textfield when the button is pressed, and then update the label once finished.
One way to do this is the following. Create a textfield in the same location as the label and initially set textField.hidden = YES;. Then implement something along these lines:
-(IBAction)editMyCell:(id)sender {
textField.text = cellLabel.text;
cellLabel.hidden = YES;
textField.hidden = NO;
[textField becomeFirstResponder];
}
and when the editing has finished, restore with
-(void)textFieldDidEndEditing:(UITextField *)textField {
cellLabel.text = textField.text;
textField.hidden = YES;
cellLabel.hidden = NO;
[textField resignFirstResponder];
}
You'll probably want to tweak this idea a bit for your situation, but it's probably the simplest thing to implement that achieves what you're after.
There is nothing in the base sdk associated with editing a UIViewController. Normally that kind of logic is defined by the programmer. But I could see someone writing a function that turns all of your UILabels in your UIView into UITextViews so the user can edit the text.
There might be sample code out there but this seems like custom code to me.

UITableViewController not highlighting cell when it's selected

Hi
I've got simple question which I don't know how to answer.
In my app I've got UITableViewController with cells. When I select one item (cell) it's getting higlighted and in other thread I'm loading chunk of data to display to the user (after load is done new VC is pushed). When doing it with thread user still can interact with application like, going back to other NavController and I do want that to happen. What I don't want to happen is that when loading isn't complete user can select other cell in table and it get's highlted. How I can prevent that (only highlit, I'm checking if there was a previous request so I'm not putting another thread untli previous request is done).
So basicly my question is, how can you foribd user from interacting with table view controller?
Set the selectionStyle of the UITableViewCell's to UITableViewCellSelectionStyleNone.
You can use the following to check if row can be selected:
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (rowSelected) {
return nil;
}
return indexPath;
}
So, you only select it if no row is selected. In your didSelectRowAtIndexPath method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
rowSelected = YES;
// call method that is going to do something and mark rowSelected = NO;
}
You can deselect the row by using
[[tableView cellForRowAtIndexPath:indexPath] setSelected:NO animated:YES];
There is a risk that your users will be confused. A highlight is not enough. There should be very clear visual feedback that a network opperation is ongoing and that different rules apply.
either push the details view immediately after the user selected a row and show an activity indicator in there.
or give the whole table view a different look while loading data for the selected row: e.g. Show activity indicator in the selected row & hide the disclosure chevrons in all the other. While doing that, you can set the selection style to 'none'

How to detect edit mode on iPhone UITableView

For my iPhone app, I have an editable (for delete) table view. I'd like to be able to detect that the user has clicked the "Edit" button. See this image: http://grab.by/It0
From the docs, it looked like if I implemented :
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
then I could detect it (although from the name of the method, I wouldn't think that). This proved not to work.
Any ideas on detecting this? The reason I want to is I want to hook up a "Delete all" button in the upper left hand corner when in delete mode.
thanks
It is probably not working as you expect because willBeginEditingRowAtIndexPath: is called before the editing starts.
If you want to check while in another method you need the editing property:
#property(nonatomic, getter=isEditing) BOOL editing
If you want to do something when the 'Edit' button is pressed you need to implement the setEditing method:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
Which you'll find in UIViewController. (Well, that's the most likely place; there are others.)
Swift
Use below code accordingly:
open var isEditing: Bool // default is NO. setting is not animated.
open func setEditing(_ editing: Bool, animated: Bool)
When subclassing a tableviewcontroller (what most people are going to be doing most of the time since you have to override it's delegate methods just to put data into it...) you can just override the setEditing:animated: method to grab editing state changes.
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
NSLog(#"Editing %i", editing);
[super setEditing:editing animated:animated];
}
That passes the state change along to the super class, but lets you jump in the middle and detect the change, or alter it if you wanted...
The setEditing:animated: examples didn't work for me (on iOS 6.1) to detect the state changes that occur when you enter and exit delete confirmation mode. It seems that setEditing:animated: is only called once, when the table view goes into edit mode, but not on state changes of the cells. After some debugger fun, I arrived at a method to detect the cell state change.
My use case is different from yours. I just wanted to hide the label when the delete button is showing so that the other cell content doesn't overlap it when the Delete button slides in. (I'm using UITableViewCellStyleValue2, the one with the blue label on the left and black label on the right.)
(In your UITableViewCell subclass)
- (void)willTransitionToState:(UITableViewCellStateMask)state {
[super willTransitionToState:state];
if (state & UITableViewCellStateShowingDeleteConfirmationMask) {
// showing delete button
[self.textLabel setAlpha:0.0f]; // <-- I just wanted to hide the label
}
}
- (void)didTransitionToState:(UITableViewCellStateMask)state {
if (!(state & UITableViewCellStateShowingDeleteConfirmationMask)) {
// not showing delete button
[self.textLabel setAlpha:1.0f]; // <-- show the label
}
}
Kendall 's answer works. I did it in following way.
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
NSLog(#"Can edit %d", tableView.editing);
if (tableView.editing == 1) {
[self.editButtonItem setTitle:EDIT_BUTTON_TITLE];
}else {
[self.editButtonItem setTitle:DONE_BUTTON_TITLE];
}
return YES;
}
That method tells you when a user is editing a Cell, not put the table into editing mode. There is a method called when editing mode is entered, to ask each cell if it can be edited:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
I don't think overriding setEditing:animated: makes sense, since you would have to subclass UITableView which is extra work and a class you need for no other reason, not to mention it would have to communicate the fact that editing had been turned on back to the controller.
One other option is to simply add the Edit button yourself - it's a built in UIBarButtonSystemItem, you can add it and then have it call your own method in which you do something specific then call setEditing:animated: on the UITableView itself.
The idea behind editing is that when editing is enabled, each cell is told to go to edit mode, and as asked if there are any specific editing controls that should be applied. So in theory there's no need to detect entry into editing mode beyond changing the appearance of cells. What are you trying to do when editing mode is entered?

Is it possible to switch uitableviewgrouped and uitableviewplain with uisegmentedcontroller?

I am developing an iphone app in which i have a segmented control that has details related to something like a book .
I want to show books menu tapping on books segment with uitableviewplain style .
but now I want another segment which should show the book vendor details with uitableviewgrouped .
how can i manage both these views with the segmented controller and mange the data source and delegate methods.
It seems that the simplest way to do that is to create two UITableViews for each purpose and show/hide them according to segmented control value.
In delegate and data source methods just check what tableview are you using, like:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (tableView == plainView){
}
if (tableView == groupView){
}
}