Objective-C Tableview Stop disabled row form moving - objective-c

I am creating an app in which I have to Reorder the Rows but I have some disabled rows which should not Move. e.g. As shown in image the 2nd and 3rd row are disabled form moving. if I try to move non disabled row they should not move to the index path of the disabled rowCheck this image

Typically you would use this method:
- (NSIndexPath *)tableView:(UITableView *)tableView
targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath
toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {
if (proposedDestinationIndexPath.row == DoNotMoveToThisRow) {
return sourceIndexPath;
}
return proposedDestinationIndexPath;
}
If you don't want a cell to move to a certain row, then just return the sourceIndexPath.

Related

NSTableView : Can I lazily fetch subsets of the table datasource?

Can I lazily fetch subsets of the NSTableView datasource?
So that I don't need to load my entire database in memory? So that when the table scrolls, the app is accessing to core data again.
At the moment I've only figured out how to enable/disable lazy fetching for the entire datasource. But I would like to know if there is a way to split the fetching in different runs".
My favorite approach in this case is following.
I keep array of loaded records in my controller. At viewDidLoad this array contains zero elements. Then i request first portions of records from database (restricted by fetch limit of 50 records for example). After records fetched I put them into this array. Also a store a flag indicating is all records loaded or not.
In my table view i always display all records from my array. If not all records loaded then I add one cell with activity indicator. When user scrolls table view down to the cell with activity indicator I request loading of next portion of records from database (using offset and fetch limit again). After records loaded I add them to my array, update flag and call reloadData on my tableView.
Two main methods of my tableViewDataSource looks like:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.allDataLoaded?self.dataArray.count?self.dataArray.count+1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row>=self.dataArray.count) {
// indicator cell
[self requestReadNext];
UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:#"IndicatorCell" forIndexPath:indexPath];
return cell;
} else {
MyCell* cell=[tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
MyObject* obj=[self.dataArray objectAtIndex:indexPath.row];
[self fillCell:cell withObject:obj];
return cell;
}
}
With a core data fetch you can specify an Offset. An offset coupled with a fetch limit gives you a range. When your tableview scrolls to the last object, do another fetch and specify your current count as the offset, and your preferred limit.

How to prevent a row of UITableView to deSelect?

When i select a row of UITableView it becomes green(customized), but when i select another row, previously selected row do not stay green. I'm reloading my table after every row selection.I've tried this
NSIndexPath *selection = [mainTable indexPathForSelectedRow];
[mainTable selectRowAtIndexPath:selection animated:NO
scrollPosition:UITableViewScrollPositionNone];
but with this code i get only most recent selected row green(selected)
I've searched for this and found some suggestions, most of them suggest to store index value of selected row in an array and use this array later, i've tried this but it did not work.
Any other suggestion or sample code will be appreciated.
Update:
A better way to achieve this would be to return nil from the UITableViewDelegate method tableView:willDeselectRowAtIndexPath: for the indexPath that you wish to stay selected.
Don't use this :
Try this add UITableViewDelegate method:
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
//use this for row u want to prevent to deSelect
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
Do this:
mainTable.allowsMultipleSelection = YES;
From the UITableView Class Reference:
allowsMultipleSelection
A Boolean value that determines whether users can select more than one row outside of editing mode.
#property(nonatomic) BOOL allowsMultipleSelection
Discussion
This property controls whether multiple rows can be selected simultaneously outside of editing mode. When the value of this property is 'YES', a check mark is placed next to each row that is tapped. Tapping the row again removes the check mark. If you call indexPathsForSelectedRows, you can get the index paths that identify the selected rows.
The default value of this property is NO.

How to dynamically create multiple UITableViews in one UIViewController

I'm new at developing with XCode and Objective-C and I hope you can help me.
The problem is, I have an UITableViewController with an UITableView (created with the InterfaceBuilder).
The cells under the section headers are expandable.
Now I want to dynamically create multiple UITableViews under the existing TableView.
The style will be the same like the existing TableView's style.
Could you tell me how it is possible to create these TableViews programmatically?
Thank you very much
Michael
From what you are saying try using a grouped table view. Check out this link for a quick overview, and go to the grouped table view section.
Edit found this example here:
Seems like it is what you are looking for. And a very cool idea also.
You'll have to just make your own custom header row and just put that as the first row of each section. Subclassing the UITableView or the headers that are on there now would probably be a huge pain and I'm not sure you can easily get actions out of them the way they work now. You could easily set up a cell to LOOK like a header, and setup the tableView:didSelectRowAtIndexPath to expand or collapse the section it is within manually.
If I were you I'd store an array of booleans corresponding the the "expended" value of each of your sections. You could then have the tableView:didSelectRowAtIndexPath on each of your custom header rows toggle this value and then reload that specific section.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
///it's the first row of any section so it would be your custom section header
///put in your code to toggle your boolean value here
mybooleans[indexPath.section] = !mybooleans[indexPath.section];
///reload this section
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
}
}
You'd then setup your number numberOfRowsInSection to check the mybooleans value and return either 1 if the section isn't expanded, or 1+ the number of items in the section if it is expanded.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (mybooleans[section]) {
///we want the number of people plus the header cell
return [self numberOfPeopleInGroup:section] + 1;
} else {
///we just want the header cell
return 1;
}
}
You would also have to update your cellForRowAtIndexPath to return a custom header cell for the first row in any section.

Didselect Method of table will not call for another row until one row action will not perform completely

give me suggestion which is related to table view selection action.
I have a view in which a table with 2 section having 2 and 3 rows.When first section 2nd row select then a picker view comes for selecting a value. similarly 2nd section each row having its picker view for selecting values.So when i am selecting a row the a picker view comes i want to make other rows didselect method will not call for rows meanwhile a picker view is showing on view and after selecting a value and done button pressed again user can select a row and picker view comes.
Use willDeselectRowAtIndexPath to return nil or the NSIndexPath. Like this :
- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([myPickerView isHidden]) return indexPath;
return nil;
}

iOS - Prevent users dragging sortable UITableViewCell to the top row

I have a UITableView within which I am allowing the user to re-order cells. All cells can be re-ordered except the top row, which must always remain as the top row.
I have used the canMoveRowAtIndexPath method to prevent the top row from being moveable. However, I can't see a way to prevent the user from dragging any of the other rows and dropping them above the top row, thus breaking my top-row requirement.
How do I stop users being able to drag other rows above the top row?
Thanks.
There is a UITableViewDelegate method that allows you to amend the destination index path if it doesn't meet your requirements. I think this should work:
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {
if (proposedDestinationIndexPath.row == 0) {
return sourceIndexPath;
}
else {
return proposedDestinationPath;
}
}