2012-12-03 44 views
0

我不知道在代码中发生什么事删除特定行的UITableView与故事板。我需要帮助我的代码,我需要一个原因如何错误。如何在Xcode 4.5.2

我选择添加项目链接ChooseViewController在ListViewController 项目列表。我需要删除项目。但我无法删除它。它有调试。

ListViewController.h

#import <UIKit/UIKit.h> 

@interface ListViewController : UITableViewController<UITableViewDelegate, UITableViewDataSource,  UISearchBarDelegate>{ 

} 
@property (strong, nonatomic)NSArray *nameCompany; 
@property (strong, nonatomic)NSArray *imageCompany; 

- (IBAction)backPage:(id)sender; 

@property (strong, nonatomic)IBOutlet UISearchBar *MyPsearch; 
@property (strong, nonatomic)IBOutlet UITableView *MyPtableView; 


-(NSMutableArray *)nameCom; 
-(NSMutableArray *)nameImage; 
-(int) getArrayCount; 
-(void)setcheck :(int)index; 

@end 

ListViewController.m

#import "ListViewController.h" 
#import "ChooseViewController.h" 
//#define ALPHA @"ABCDEFGHIJKLMNOPQRSTUVWXYZ" 
@interface ListViewController() 

@end 

@implementation ListViewController 
int checkArray[3]; 
@synthesize MyPsearch; 
@synthesize MyPtableView; 
@synthesize nameCompany =_nameCompany; 
@synthesize imageCompany =_imageCompany; 

NSMutableArray *coms; 
NSMutableArray *Imagecoms; 

NSMutableArray *totalString; 
NSMutableArray *filteredString; 


BOOL isFiltered; 
int check; 
UIButton *additem; 


-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{ 
    if (searchText.length == 0) { 
     isFiltered = NO; 
    } 
    else{ 
     isFiltered = YES; 
     filteredString = [[NSMutableArray alloc]init]; 

     for (NSString *str in totalString) { 
      NSRange stringRange = [str rangeOfString:searchText options:NSCaseInsensitiveSearch]; 
      if (stringRange.location != NSNotFound) { 
       [filteredString addObject:str]; 
      } 
     } 
    } 
    [self.MyPtableView reloadData]; 
} 


-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar 
{ 
    [searchBar resignFirstResponder]; 
} 


- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.MyPsearch.delegate = self; 
    self.MyPtableView.delegate = self; 
    self.MyPtableView.dataSource = self; 

    totalString = [[NSMutableArray alloc]initWithObjects:@"UOB Bank", @"US airways", @"Bank of America", nil]; 
    self.nameCompany = [[NSMutableArray alloc]initWithArray:totalString]; 
    self.imageCompany = [[NSMutableArray alloc]initWithObjects:@"UOB.png", @"us-airways.png", @"bank-of-america.png", nil]; 

    if (check == 0) { 
     coms = [[NSMutableArray alloc]init]; 
     Imagecoms = [[NSMutableArray alloc]init]; 
     check = 1; 
    } 



     //[self.createSectionList:wordarray]; 
    // Uncomment the following line to preserve selection between presentations. 
    // self.clearsSelectionOnViewWillAppear = NO; 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 
} 
-(void)addNewItem:(UIButton *)sender{ 
    //การadd Item เก็บค่าไว้ 
    [sender setHidden:YES]; 
    [coms addObject:[totalString objectAtIndex:sender.tag-100]]; 
    [Imagecoms addObject:[_imageCompany objectAtIndex:sender.tag-100]]; 

    checkArray[sender.tag-100]= 1; 


} 

-(NSMutableArray *)nameCom{ 
    return coms; 
} 

-(NSMutableArray *)nameImage{ 
    return Imagecoms; 
} 



- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
//#warning Potentially incomplete method implementation. 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
//#warning Incomplete method implementation. 
    // Return the number of rows in the section. 
    if (isFiltered) { 
     return [filteredString count]; 
    } 
    return [totalString count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //static NSString *CellIdentifier = @"ListCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ListCell"]; 
    //if (!cell) { 
     // cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    //} 
    if (!isFiltered) { 

     cell.textLabel.text = [totalString objectAtIndex:[indexPath row]]; 
     cell.imageView.frame = CGRectMake(0, 0, 54, 33); 
     UIImage *PlacePhoto3 = [UIImage imageNamed:[self.imageCompany objectAtIndex:[indexPath row]]]; 
     cell.imageView.image = PlacePhoto3; 
     //ปุ่ม บวก 
     if (checkArray[indexPath.row] == 0) { 
     additem = [UIButton buttonWithType:UIButtonTypeContactAdd]; 
     additem.frame = CGRectMake(275, 11, 29, 29); 
     additem.tag = 100+indexPath.row; 
     [additem addTarget:self action:@selector(addNewItem:) forControlEvents:UIControlEventTouchUpInside]; 

     [cell addSubview:additem]; 
     } 


    }else{ 
     cell.textLabel.text = [filteredString objectAtIndex:[indexPath row]]; 
     cell.imageView.frame = CGRectMake(0, 0, 54, 33); 
     UIImage *PlacePhoto3 = [UIImage imageNamed:[self.imageCompany objectAtIndex:[indexPath row]]]; 
     cell.imageView.image = PlacePhoto3; 

     //ปุ่มบวก 
     additem =[UIButton buttonWithType:UIButtonTypeContactAdd]; 
     additem.frame = CGRectMake(275, 11, 29, 29); 
     additem.tag = 100+indexPath.row; 
     [additem addTarget:self action:@selector(addNewItem:) forControlEvents:UIControlEventTouchUpInside]; 

     [cell addSubview:additem]; 


    } 

    return cell; 
} 


-(void)setcheck :(int)index{ 
    checkArray[index] = 0; 
} 


- (IBAction)backPage:(id)sender { 
    ChooseViewController *cvc = [[ChooseViewController alloc]init]; 
    [cvc viewDidLoad]; 
} 

ChooseViewController.h

#import <UIKit/UIKit.h> 

@interface ChooseViewController : UITableViewController 
@property (strong, nonatomic) IBOutlet UITableView *tableView; 



@end 

ChooseViewController.m

#import "ChooseViewController.h" 
#import "ListViewController.h" 
#import "MultiViewController.h" 

@interface ChooseViewController() 

@end 

@implementation ChooseViewController 
NSMutableArray *Imagename; 
NSMutableArray *namecoms; 
int tableCount; 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 

    } 
    return self; 
} 



- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    tableCount = 0 ; 




    self.navigationItem.leftBarButtonItem = self.editButtonItem; 
    Imagename = [[NSMutableArray alloc]init]; 
    namecoms = [[NSMutableArray alloc]init]; 

    ListViewController *list = [[ListViewController alloc]init]; 

    Imagename = [[NSMutableArray alloc]initWithArray:[list nameImage]]; 
    namecoms = [[NSMutableArray alloc]initWithArray:[list nameCom]]; 
    tableCount = namecoms.count; 

    // Uncomment the following line to preserve selection between presentations. 
    // self.clearsSelectionOnViewWillAppear = NO; 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 
} 




- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
//#warning Potentially incomplete method implementation. 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
//#warning Incomplete method implementation. 
    // Return the number of rows in the section. 
    return tableCount; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellChoose"]; 
    if (!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellChoose"]; 
    } 


    cell.textLabel.text = [namecoms objectAtIndex:indexPath.row]; 
    UIImage *Photo3 = [UIImage imageNamed:[Imagename objectAtIndex:indexPath.row]]; 
    cell.imageView.image = Photo3; 

    return cell; 
} 




// Override to support conditional editing of the table view. 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not 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 
{ 

    // Request table view to reload 

    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source ฟังชั่น delete cell ออก 

     //[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]withRowAnimation:YES]; 


     NSLog(@"%@",[namecoms objectAtIndex:indexPath.row]); 
     //[Imagename removeObject:namecoms]; 
     //[namecoms removeObject:Imagename]; 
     //ListViewController *objList = [[ListViewController alloc]init]; 
     // [objList setcheck:indexPath.row]; 
     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
      [Imagename removeObjectAtIndex:indexPath.row]; 
      [namecoms removeObjectAtIndex:indexPath.row]; 
     [tableView reloadData]; 


} 
    else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    } 
} 


- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ 
    if ([segue.identifier isEqualToString:@"chooseMulti"]) 
    { 
    MultiViewController *MTC = [[MultiViewController alloc]init]; 
    MTC = [segue destinationViewController]; 
    NSIndexPath *path = [self.tableView indexPathForSelectedRow]; 
    NSString *placename = [namecoms objectAtIndex:[path row]]; 
    MTC.comnumber = path.row; 
    MTC.companyName = placename; 
    } 
} 






/* 
// Override to support rearranging the table view. 
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath 
{ 
} 
*/ 

/* 
// Override to support conditional rearranging of the table view. 
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the item to be re-orderable. 
    return YES; 
} 
*/ 

#pragma mark - Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Navigation logic may go here. Create and push another view controller. 
    /* 
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil]; 
    // ... 
    // Pass the selected object to the new view controller. 
    [self.navigationController pushViewController:detailViewController animated:YES]; 
    */ 
} 



@end 

跑吧!响应......

#import <UIKit/UIKit.h> 

#import "CTASIAAppDelegate.h" 

int main(int argc, char *argv[]) 
{ 
    @autoreleasepool { 
     return UIApplicationMain(argc, argv, nil, NSStringFromClass([CTASIAAppDelegate class])); 
    } 
} 

感谢you.for帮助我。 :)


我做过。它同样的错误

#import <UIKit/UIKit.h> 

#import "CTASIAAppDelegate.h" 

int main(int argc, char *argv[]) 
{ 
    @autoreleasepool { 
     return UIApplicationMain(argc, argv, nil, NSStringFromClass([CTASIAAppDelegate class])); 
    } 
} 

什么恰巧.......................(TT)

回答

2

在你的代码在这里:

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
[tableView reloadData]; 
[Imagename removeObjectAtIndex:indexPath.row]; 
[namecoms removeObjectAtIndex:indexPath.row]; 

首先删除对象,然后重新加载数据。

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
[Imagename removeObjectAtIndex:indexPath.row]; 
[namecoms removeObjectAtIndex:indexPath.row]; 
[tableView reloadData]; 

在您从数据中移除对象之前,您重新加载数据,导致代码崩溃。

+0

正确的顺序是:首先从模型数组中删除对象,然后调用'deleteRowsAtIndexPaths'。 'reloadData'不是必需的。 –

+0

我做到了。但同样的错误。 –