Why isn't reorder control showed?

Discussion in 'iOS Programming' started by mikezang, Jul 23, 2010.

  1. macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #1
    I made a simple table view with contents and code as below, but I can't view reorder control, can you tell me where my code is not correct?
    row1: show UITableViewCellAccessoryDisclosureIndicator
    row2: show UITableViewCellAccessoryDetailDisclosureButton
    row3: show UITableViewCellAccessoryCheckmark
    row4: show UITableViewCellEditingStyleNone(reorder control) in edit mode
    row5: show UITableViewCellEditingStyleInsert in edit mode
    row6: show UITableViewCellEditingStyleDelete in edit mode
    Code:
    #import "FirstViewController.h"
    @implementation FirstViewController
    - (IBAction) toggleEdit:(id)sender {
    	[self.tableView setEditing:!self.tableView.editing animated:YES];
    	if (self.tableView.editing) {
    		[self.navigationItem.rightBarButtonItem setTitle:@"Done"];
    	}
    	else {
    		[self.navigationItem.rightBarButtonItem setTitle:@"Edit"];
    	}
    }
    - (void)viewDidLoad {
    	self.title = @"First Level";
    	UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered target:self action:@selector(toggleEdit:)];
    	self.navigationItem.rightBarButtonItem = editButton;
    	[super viewDidLoad];
    }
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    	return 12;
    }
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    	static NSString *First = @"First";
    	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:First];
    	if (cell == nil) {
    		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:First] autorelease];
    	}
    	NSUInteger row = [indexPath row];
    	cell.textLabel.text = [[NSString alloc] initWithFormat:@"Row is %d", row + 1];
    	if (row % 6 == 0) {
    		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    	}
    	else if (row % 6 == 1) {
    		cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    	}
    	else if (row % 6 == 2) {
    		cell.accessoryType = UITableViewCellAccessoryCheckmark;
    	}
    	else if (row % 6 == 3) {
    		cell.showsReorderControl = YES;
    	}
    	return cell;
    }
    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    	NSUInteger row = [indexPath row];
    	if (row % 6 == 4) {
    		return UITableViewCellEditingStyleInsert;
    	}
    	else if (row % 6 == 5) {
    		return UITableViewCellEditingStyleDelete;
    	}
    	else {
    		return UITableViewCellEditingStyleNone;
    	}
    }
    - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    	NSUInteger row = [indexPath row];
    	if (row % 6 == 3) {
    		return YES;
    	}
    	else {
    		return FALSE;
    	}
    }
    @end
    
     

    Attached Files:

  2. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #2
    I got the reason, but I just followed book "Beginning iPhone 3 Development" that said as below at page 313, I thought only have this method is enough, now I know the key method is moveRowAtIndexPath:
    Next comes the method tableView:canMoveRowAtIndexPath:. This method gets called for each row, and it gives you the chance to disallow the movement of specific rows. if you return NO from this method for any row, the reorder control will not be shown for that row, and the user will be unable to move it from its current position. We want to allow full reordering, so we just return YES for every row.
     
  3. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    Did you come across this after you started this thread? If so, be patient. You normally don't want to run your code until that book suggests you do.
     
  4. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #4
    I didn't full follow that book, I just follow that concept to build a NavigationController app, asI just want to test to show reorder control, and not really move order, so that I didn't use that moveRowAtIndexPath at first.

    Like you said, I should check class reference before I post thread here, I will check reference firstly next time.
     
  5. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    tableView:moveRowAtIndexPath:toIndexPath: or tableView:canMoveRowAtIndexPath:?
     
  6. thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #6
    tableView:moveRowAtIndexPath:toIndexPath:
     

Share This Page