重新排序tableview单元格问题

时间:2015-09-22 12:41:05

标签: ios

我刚刚在Core Data中创建了一个评估。我的过程是使用tableview中的按钮重新排序它们。

我在tableview中使用了我的动态按钮但我无法在tableview单元格中重新排序它们请帮助我。它就像拖放一样。

     table=[[UITableView alloc]init];
        table.frame=CGRectMake(prev.frame.size.width*1.2, questionlabel.frame.size.height*1.5,self.view.frame.size.width/1.6, self.view.frame.size.height/2);
        table.backgroundColor=[UIColor blueColor];
        [table setSeparatorStyle:UITableViewCellSeparatorStyleNone];

        [self.view addSubview:table];

    -(void)addbutton:(NSString *)desc bid:(int)bid{


    [[QBFlatButton appearance] setFaceColor:[UIColor colorWithWhite:0.75 alpha:1.0] forState:UIControlStateNormal];
    [[QBFlatButton appearance] setSideColor:[UIColor colorWithWhite:0.55 alpha:1.0] forState:UIControlStateNormal];

    optionButton = [QBFlatButton buttonWithType:UIButtonTypeCustom];
    optionButton.faceColor = [UIColor colorWithRed:86.0/255.0 green:161.0/255.0 blue:217.0/255.0 alpha:1.0];
    optionButton.sideColor = [UIColor colorWithRed:79.0/255.0 green:127.0/255.0 blue:179.0/255.0 alpha:1.0];
    optionButton.radius = 4.0;
    optionButton.margin = 4.0;
    optionButton.depth = 3.0;
    [optionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    optionButton.titleLabel.font=[UIFont systemFontOfSize:12];
    optionButton.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
    // you probably want to center it
    optionButton.titleLabel.textAlignment = NSTextAlignmentCenter;
//    [optionButton addTarget:self
//                     action:@selector(dynamicclick:)
//           forControlEvents:UIControlEventTouchUpInside];
    optionButton.tag=bid;
    [optionButton setTitle:desc forState:UIControlStateNormal];
    optionButton.frame = CGRectMake(5,bid*55,table.frame.size.width-10, 50);
    [table addSubview:optionButton];



}


- (void)initTableItem {
    self.items = [NSMutableArray array];
    [self.items addObject:optionButton];

}



- (void)logItems {
    int index = 0;
    for ( NSString *str in self.items ) {
        NSLog(@"%d: %@", index++, str);
    }
}
#pragma mark - UITableViewDataSource, UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.items count];
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:@"Cell"];

    if ( cell == nil ) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];

    }

     cell.layoutMargins=UIEdgeInsetsZero;


    [cell addSubview:optionButton];

    return cell;
}

#pragma mark - Edit Mode
- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleNone; // No Delete icon



}

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{

    [table deselectRowAtIndexPath:indexPath animated:YES];


}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Can move cell
    return YES;
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {

    NSUInteger origins = sourceIndexPath.row;  // Original position
    NSUInteger to = destinationIndexPath.row; // Destination position
    NSLog(@"Origin %lu, To %lu", (unsigned long)origins, (unsigned long)to);
    NSString *swap = [self.items objectAtIndex:origin];// Item

    [swap shouldGroupAccessibilityChildren];

}

0 个答案:

没有答案