好的,所以我的项目(确实有Cocos2d)有3个UITableViews。到目前为止,我只关心第一个,这就是为什么我只粘贴第一个代码的原因。问题是,每当我使用应用程序时,表格视图都会显示,并显示所有内容。编辑/完成按钮也会显示在右下角。但是,每当我尝试按下编辑按钮时,按钮切换到完成,但单元格附近没有删除图标,也无法删除单元格。我尝试了很多解决方案,但似乎都没有。提前谢谢。
-(id)init{
if (self == [super init]) {
// Create initialized instance of UITabBarController
tabController = [[UITabBarController alloc] init];
// Create initialized instance of NSMutableArray to hold our UINavigationControllers
tabArray = [[NSMutableArray alloc] init];
// Create first UIViewController
questionController = [[UIViewController alloc] init];
[self makeQuestionTable];
[questionController setTitle:@"Question"];
// Initialize the UINavigationController
navigationController = [[UINavigationController alloc] initWithRootViewController:questionController];
questionController.navigationItem.rightBarButtonItem = questionController.editButtonItem;
questionTableView.allowsSelection = YES;
questionTableView.allowsMultipleSelectionDuringEditing = YES;
questionTableView.allowsSelectionDuringEditing = YES;
// Add UINavigationController to you tabs
[tabArray addObject:navigationController];
// Release UIViewController and UINavigationController
[questionController release], [navigationController release];
// Create second UIViewController
answerController = [[UIViewController alloc] init];
[self makeAnswerTable];
[answerController setTitle:@"Answer"];
// Initialize the UINavigationController
navigationController = [[UINavigationController alloc] initWithRootViewController:answerController];
// Add UINavigationController to you tabs
[tabArray addObject:navigationController];
// Release UIViewController and UINavigationController
[answerController release], [navigationController release];
// Create third UIViewController
colorController = [[UIViewController alloc] init];
[self makeColorTable];
[colorController setTitle:@"Color"];
// Initialize the UINavigationController
navigationController = [[UINavigationController alloc] initWithRootViewController:colorController];
// Add UINavigationController to you tabs
[tabArray addObject:navigationController];
// Release UIViewController and UINavigationController
[colorController release], [navigationController release];
// Add the tabs to the UITabBarController
[tabController setViewControllers:tabArray];
// Add the view of the UITabBarController to the window
[[[CCDirector sharedDirector]view]addSubview:tabController.view];
}
return self;
}
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
[questionTableView setEditing:editing animated:animated];
[questionController setEditing:editing animated:animated];
}
-(void)makeQuestionTable{
questionTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain];
questionTableView.backgroundColor = [UIColor whiteColor];
questionTableView.delegate = self;
questionTableView.dataSource = self;
questionTableView.tag = 1;
[questionTableView reloadData];
[questionController.view addSubview:questionTableView];
}
-(void)makeAnswerTable{
answerTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 400) style:UITableViewStylePlain];
answerTableView.backgroundColor = [UIColor whiteColor];
answerTableView.delegate = self;
answerTableView.dataSource = self;
answerTableView.tag = 2;
[answerTableView reloadData];
[answerController.view addSubview:answerTableView];
}
-(void)makeColorTable{
colorTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 400) style:UITableViewStylePlain];
colorTableView.backgroundColor = [UIColor whiteColor];
colorTableView.delegate = self;
colorTableView.dataSource = self;
colorTableView.tag = 3;
[colorTableView reloadData];
[colorController.view addSubview:colorTableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (currentTab == 1) {
return [appDelegate.questionArray count];
}
else if (currentTab == 2) {
return [appDelegate.answerArray count];
}
else if (currentTab == 3) {
return [appDelegate.colorArray count];
}
else{
return 0;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView.tag == 1) {
static NSString *simpleTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [appDelegate.questionArray objectAtIndex:indexPath.row];
return cell;
NSLog(@"Question");
}
if(tableView.tag == 2){
static NSString *simpleTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [appDelegate.answerArray objectAtIndex:indexPath.row];
return cell;
NSLog(@"Answer");
}
if(tableView.tag == 3){
static NSString *simpleTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [appDelegate.colorArray objectAtIndex:indexPath.row];
return cell;
NSLog(@"Color");
}
}
- (UITableViewCellEditingStyle)tableView:(UITableView*)tableView
editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath {
UITableViewCell *cell = [questionTableView cellForRowAtIndexPath:indexPath];
if ([cell isEditing]) {
NSLog(@"YES");
}
else{
NSLog(@"NO");
}
return UITableViewCellEditingStyleDelete;
}
// This only needs to be implemented if you are going to be returning NO
// for some items. By default, all items are editable.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return YES if you want the specified item to be editable.
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//add code here for when you hit delete
[questionTableView delete:indexPath];
}
}
答案 0 :(得分:0)
将tableView的属性“editing”设置为“YES”。像:
questionTableView.editing = YES;
删除此行:
questionTableView.allowsMultipleSelectionDuringEditing = YES;
选择时或将allowsMultipleSelectionDuringEditing
设置为“NO”。
答案 1 :(得分:0)
在edit
按钮操作中,您需要将setEditing
设为YES
。
[tableView setEditing:YES animated:YES];
答案 2 :(得分:0)
你在- (void)setEditing
之前错过了几个结束大括号,而不应该是你的init方法return self;