PDA

View Full Version : Why isn't reorder control showed?




mikezang
Jul 23, 2010, 09:16 AM
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

#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



mikezang
Jul 23, 2010, 10:06 AM
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.

dejo
Jul 23, 2010, 10:54 AM
I got the reason...
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.

mikezang
Jul 23, 2010, 12:47 PM
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.

dejo
Jul 23, 2010, 12:54 PM
tableView:moveRowAtIndexPath:toIndexPath: or tableView:canMoveRowAtIndexPath:?

mikezang
Jul 23, 2010, 01:33 PM
tableView:moveRowAtIndexPath:toIndexPath: or tableView:canMoveRowAtIndexPath:?
tableView:moveRowAtIndexPath:toIndexPath: