I have 2 sections in my UITableView. I want the 2nd section to be movable but the 1st section of cells not.
Specifying canEditRowAtIndexPath and canMoveRowAtIndexPath doesn't help - the first section cells although not showing drag controls, they still change places if a cell from the 2nd section is dragged over.
Is there a workaround for this?
Try implementing the targetIndexPathForMoveFromRowAtIndexPath method and forcing the row from the second section back to its original place if user tries to move it to the first section:
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
if (proposedDestinationIndexPath.section == 0)
{
return sourceIndexPath;
}
else
{
return proposedDestinationIndexPath;
}
}
Related
Has anybody come across this situation?
When I am re-ordering the cells from the bottom most to top most I get a crash. These crashes happens when iSourceIndexPath.row becomes 0. I am wondering how is my iSourceIndexPath.row becoming 0 as this is the last row I am picking always. Any clue?
- (NSIndexPath *)tableView:(UITableView *)iTableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)iSourceIndexPath toProposedIndexPath:(NSIndexPath *)iProposedDestinationIndexPath {
NSIndexPath *aReturnIndexPath = iProposedDestinationIndexPath;
if(iProposedDestinationIndexPath.row == 0) {
aReturnIndexPath = iSourceIndexPath;
}
NSLog(#"iProposedDestinationIndexPath=%d iSourceIndexPath=%d aReturnIndexPath=%d",iProposedDestinationIndexPath.row,iSourceIndexPath.row,aReturnIndexPath.row);
return aReturnIndexPath;
}
I forgot to reload the table after cells are moved. You should do this apart from your regular handling of cells in it.
- (void)tableView:(UITableView *)iTableView moveRowAtIndexPath:(NSIndexPath *)iFromIndexPath toIndexPath:(NSIndexPath *)iToIndexPath {
[self.tableView reloadData]
}
Hello
I am developing an app which is using a tableview with two sections(instead of radio buttons).
So I want these section to act like radio buttons. To accomplish this, I have to implement an "if" in the
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
I want to use something like: if (section == "A").
How to do that?
Also is this the right way to make the sections of a table acting like a different tables?
Hmm... that sounds like an odd set up, but irrespective, you simply use the provided indexPath to obtain the section/row.
i.e.: indexPath.section will point to the selected section and indexPath.row will point to the row.
For more information, the Table View Programming Guide for iOS is a good start as it covers all of the above in quite a bit of detail.
UPDATE
In terms of highlighting multiple cells, I'd have thought that you'd want to create your own concept of highlighting, which you'd toggle on and off when the user selects the cell.
That said, you could also mark selected cells via the UITableViewCellAccessoryCheckmark as per the existing Is it possible to configure a UITableView to allow multiple-selection? question/answer.
I don't know what you mean by "section to act like radio buttons" but to distinguish between sections in tableView:didSelectRowAtIndexPath: use indexPath.section:
if (indexPath.section == 0) {
// first section
} else if (indexPath.section == 1) {
// second section
}
you must need to use indexPath.section: to distinguish between your table sections
So your code should be like below .
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
{
if (indexPath.section == 0)
{
//first section
}
else if (indexPath.section == 1)
{
//second section
}
}
I try to set up a tableView. I use standard cells for all sections' rows except in the last section (containing one row). Thus, I would also like to use the standard layout for all those sections except that special one.
A short example is the following, my "special" cell is in section 3 (there is only one row):
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if (section == 3)
return 5;
return **????**;
}
At ??? I would like to return the width calculated from UITableView (just as if I did not implement the method).
[super self:tableView
heightForHeaderInSection:(NSInteger)section];
does not work. I know I can access
[tableView setionHeaderHeight]
which is by default 10 and obviously does not take into account that I have section headings for the other sections, which will require additional space. I tried that, but it will then get the sections too close (see screenshot):
(Note: the section I am interested in is the one which does not look like a cell: the one with the dates (invisible background)).
So, the easiest thing would be to hand over the layout to the standard implementation which is perfect - except for section3.
What are my options?
Just in case: there is a new constant introduced in iOS 5, called UITableViewAutomaticDimension. As the documentation says, you should return it from your delegate method when you want UITableView to use a default value.
So, the code for your case would be:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if (section == 3) {
return 5;
} else {
return UITableViewAutomaticDimension;
}
}
You seem a bit confused about heightForHeaderInSection - it returns the height of a table section header (this is the "title" of a table section), not a row. iOS calls this method to ask for the height of just a single section header, irrespective of any other section headers there might be.
If you want to use the default, just return [tableView sectionHeaderHeight] for any section other than 3 - you don't need to "take into account that [you] have other section headers", as it's asking for the height of the header for section alone. It will ask again for the heights of others (and compute the relative positions with of rows and other sections automatically).
You do not have a super implementation tableView:heightForHeaderInSection: since you are not subclassing any abstract base implementation for UITableViewDelegate. The table view is instead decided if the default height should be used by inspecting your delegate implementation to see if the method is available.
It is a quite a huge concept to wrap your head around, especially if coming from Java or C#. Methods in Objective-C protocols can be optional, and their absence means use default.
Your method should probably be implemented as:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if (section == 3) {
return 5;
} else {
return 36;
}
}
The default height for grouped and plain tableviews are different (22points for plain). The default values are not exposed by UITableView, not even as private methods. File bug at http://bugreport.apple.com to make this a public constant.
After overriding heightForHeaderInSection and doing a side-by-side comparison, the height for the header in the first row is larger than the rest. This isn't pixel perfect, but it's very close:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 0 ) {
return 46.0;
} else if (section == myCustomRow) {
return 12345.0; // custom height
} else {
return 36.0;
}
}
I was wondering if there is a way to make a UITableView editable in a way where the user is able to rearrange (move) individual TableViewCells, BUT without the "Remove-Cell"-Feature to appear.
thank you for your help
sam
Have you tried implementing the editingStyleForRowAtIndexPath method and returning UITableViewCellEditingStyleNone?
You change the if condition based on your requirement
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
// in below if your write your condition on which delete is enable or disable
if ([[friednListArr objectAtIndex:indexPath.row][#"id"] isEqualToString:user.userId]) {
return UITableViewCellEditingStyleNone;
}
else{
return UITableViewCellEditingStyleDelete;
}
}
I have a UITableView With Two Sections. First section has one row and the section one has variable number of rows which varies according to data entry in database.
I want that user cant delete the cell from first section but he is able to delete the cells from the second section.
I have implemented the commitEditingStyle method for the tableview but the problem is it allows the user to delete the row from first section.
I can put some flag to check it in commitEditingStyle but what I want to do is just block it to show the editing button. That is user wont be able to see the delete button when he swips his or her finger on the table cell.
I did set the property editing=false but cant set editingStyle since it is readonly property. Setting editing=false doesnt work.
tnx.
Yeah this works.
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.section) {
case 0:
return UITableViewCellEditingStyleNone;
break;
case 1:
return UITableViewCellEditingStyleDelete;
break;
default:
return UITableViewCellEditingStyleNone;
break;
}
}
Have you tried setting the tableViewCells editing style to none for the first section cells? I think that should work for you