Enable UITableView edit only in a specified section - ios

I have a tableView with 4 sections. I want to enable editing (move/drag/ rearrange ) the cells only within the forth section. When I set: tableView.editing = YES I get all the table view to be in editing mode. This How to limit UITableView row reordering to a section helped me as I can now rearrange cells from a section only within their "root" section.
What I mean is if the cell is in the 1st section then I only get to rearrange within the 1st section and not the others. My goal is to enable editing only with The 4th section therefore putting in Move/drag Mode only the cells within the 4th Section. Does anyone know how I can do this?

You can achieve this by implementing this delegate method
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 3) {
return YES;
} else {
return NO;
}
}

Related

How to disable user interaction of section index for UITableView?

I have implemented a section index, letters at the right of the table view that allow quickly jumping to a section, in my app. I would like to disable user interaction with that section index in some situations, but there is no property on UITableView that allows directly accessing the section index.
How can I disable user interaction for section indexes?
I have disabled interaction with the table itself so that you cannot scroll or tap any cell, but this still allows interacting with the section indexes, so it will scroll to each section upon tapping a section icon.
self.tableView.userInteractionEnabled = NO;
If it's not possible to disable interaction with the section index itself, is there a way to prevent changing the scroll position of the table? That's exactly what I wanted to obtain.
As an alternative, if you wish to allow scrolling - so users can see what's there - but prevent them from making selections, etc. you can do the following instead:
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.tableViewIsDidabled)
return nil;
else
return indexPath;
}
and, setting the sectionForSectionIndexTitle to nil.
I found a solution.
Keep a BOOL to know when the section indexes should be disabled.
Then in tableView:sectionForSectionIndexTitle:atIndex: just return -1 to prevent scrolling when tapping indexes.
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
if (self.tableViewIsDisabled) {
return -1;
}
...
}
UITableView *tab;
mm to disable scroll..
tab.scrollEnabled = NO;
if you wanna use it in certain situations..
you can try with a powerful..
if(...)
tab.scrollEnabled = NO;

Mixing static and dynamic sections in a grouped table view

I need a grouped UITableView similar to the one for Twitter accounts in Settings app:
That is, a sort of form or menu where some of the sections have a beforehand known set of static cells, and some other sections have to be dynamic and allow inserting additional rows the same way the "Add Account" does here. I'm managing the UITableView in a .xib file. For the static cells, I have separated .xib files that I can load within the cellForRowAtIndexPath: method in the view controller.
How should I handle this kind of table? I donĀ“t find any example code.
How the cellForRowAtIndexPath: method should look like? May I need to keep strong properties for the static cells? Would it be better to design each static cell directly within the same .xib file where the table view is, and to set outlets for them? (Though this does not allow to reuse my custom cells design...)
I need some guidelines for achieving this and correctly managing cells and memory. Thanks in advance
Dynamic prototype cells can behave like static ones if you just return the cell without adding any content in cellForRowAtIndexPath, so you can have both "static like" cells and dynamic ones (where the number of rows and the content are variable) by using dynamic prototypes.
In the example below, I started with a table view controller in IB (with a grouped table view), and changed the number of dynamic prototype cells to 3. I adjusted the size of the first cell to 80, and added a UIImageView and two labels. The middle cell is a Basic style cell, and the last one is another custom cell with a single centered label. I gave them each their own identifier. This is what it looks like in IB:
Then in code, I did this:
- (void)viewDidLoad {
[super viewDidLoad];
self.theData = #[#"One",#"Two",#"Three",#"Four",#"Five"];
[self.tableView reloadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 1)
return self.theData.count;
return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0)
return 80;
return 44;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
if (indexPath.section == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:#"TitleCell" forIndexPath:indexPath];
}else if (indexPath.section == 1) {
cell = [tableView dequeueReusableCellWithIdentifier:#"DataCell" forIndexPath:indexPath];
cell.textLabel.text = self.theData[indexPath.row];
}else if (indexPath.section == 2) {
cell = [tableView dequeueReusableCellWithIdentifier:#"ButtonCell" forIndexPath:indexPath];
}
return cell;
}
As you can see, for the "static like" cells, I just return the cell with the correct identifier, and I get exactly what I set up in IB. The result at runtime will look like your posted image with three sections.
Static is just a provision on top of dynamic layout. Basically static is a WYSIWYG.
If you are not resistant to experiment with values, I would recommend go dynamic. There are hundreds of examples available, such as this and this.
As you go further, you would see yourself diverging towards two options when customizing dynamic table views:
Subclass UITableViewCell (more effort , but good in the long run). Again, follow this.
Play with UITableViewCell properties inside cellForRowAtIndexPath: (less effort and quick result but may or may not be performance-friendly due to possible redrawing)
The possibilities are endless, such as here where cell background view is customized.

uitableview drag and drop with not moving cells

I would like to make use of the drag and drop reordering of tableview cells, but I have one problem:
I would like to keep my first row in place!
Here is the canMoveRowAtIndexPath delegate function:
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row==0) {
return NO;
}
return YES;
}
If I do it like this, the first row can not be dragged, BUT you can still drag another row on top of it! Is there any way to prevent cells from dropping 'above' this row?
Your answer is hear,
You can use these step.
Examples of Moving a Row
check if it is a drag and cancel the scrolling if it is = [uitableview setScrollEnabled:NO]
read this thread, it will help you i hope
Tutorial on How to drag and drop item from UITableView to UITableView

UITableView returning wrong indexPath when moving cells between sections

I'm creating a list app where the user can check off items, the checked items move to the bottom (a different section in the UITableView) when checked after a short delay. The user can "uncheck" the item by tapping it and it moves back to the original section.
My problem is that when the cells are moving between sections like this it seems the UITableView get "confused" about where which cells are.
When I NSLog the IndexPath.row of the cell I'm interacting with it returns a different row than I'm touching. I use [self.tableView moveRowAtIndexPath:from_indexPath toIndexPath:to_indexPath]; to move the cells between sections. Here's some of my code:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
switch (indexPath.section) {
case SECTION_ACTIVE:{
ItemData *dataForCell = [activeList objectAtIndex:indexPath.row];
NSLog(#"%# \t %d",dataForCell.title, indexPath.row);
if (dataForCell.isHeader) {
NSLog(#"%d",indexPath.row);
return 70;
}
break;
}
default:
break;
}
return 55;
}
Is there any way to work around this? Or to fix this?
You can tag your table cells and can re-check it when you move your cell from one section to another.

Table view cell expandable iOS

I want a table view with only cels, and when you click on a cell it should expand and show more info of the clicked cell.
I've seen quite some topics on this, but the most of them are linking to Table View Animations and Gestures on the apple developer page. Which does it in a different way. They use header sections, but I want to use the cell which is expandable for layout reasons.
I already tried several things mainly with
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (isSearching && indexPath.row == selectedIndex) {
return 110;
}
else {
return rowHeight;
}
When I Click on the cell, the cell is expanded but the info in that cell stays the same. Also the heigth of the cell when expanded should be related to the amount of text in the details.
Thnx!
You can achieve this through the use of custom cells. Create two custom cells, one for the normal row and other for the expanded row. When the user touches a particular cell, you can record it's indexPath and reload the tableView. While reloading you can change the height of this selected row using the code that you've just posted(increasing the height of only the selected cell). This would give an effect of expanding cell.

Resources