Uitableview 为什么我的可变数组和表视图的行为会出人意料?

Uitableview 为什么我的可变数组和表视图的行为会出人意料?,uitableview,ios6,nsmutablearray,didselectrowatindexpath,Uitableview,Ios6,Nsmutablearray,Didselectrowatindexpath,我正在构建一个登录应用程序。它有两个表视图。一个列出以前访问过的人(现有姓名),另一个列出当前登录的人(姓名) 在我的代码中的某些点上,唯一在访问时不会使程序崩溃的可变数组是名称 名称和现有名称似乎也以某种方式颠倒过来。当我试图从名称中删除时,程序崩溃。当我从现有名称中删除时,更改会反映在tableView2中,但tableView2应该与名称关联 在应用程序的当前状态下,除访问任何公司阵列外,其他一切都“正常”。我在工作中使用引号,因为名称和现有名称被反向使用 如果您能深入了解问题的原因,我们

我正在构建一个登录应用程序。它有两个表视图。一个列出以前访问过的人(现有姓名),另一个列出当前登录的人(姓名)

在我的代码中的某些点上,唯一在访问时不会使程序崩溃的可变数组是名称

名称和现有名称似乎也以某种方式颠倒过来。当我试图从名称中删除时,程序崩溃。当我从现有名称中删除时,更改会反映在tableView2中,但tableView2应该与名称关联

在应用程序的当前状态下,除访问任何公司阵列外,其他一切都“正常”。我在工作中使用引号,因为名称和现有名称被反向使用

如果您能深入了解问题的原因,我们将不胜感激

.h:

#导入
@界面ViewController:UIViewController{
UITableView*表格视图;
}
@属性(弱,非原子)IBOutlet UITextField*nameField;
@属性(弱、非原子)IBUITExtField*companyField;
@属性(非原子,保留)NSMutableArray*名称;
@财产(非原子,保留)NSMUTABLEARRY*公司;
@属性(非原子,保留)NSMutableArray*现有名称;
@财产(非原子,保留)NSMutableArray*现有公司;
-(iAction)添加:(id)发件人;
-(iAction)addExisting:(id)发送方;
-(iAction)删除:(id)发送方;
-(iAction)提交:(id)发件人;
@属性(强,非原子)IBUITableView*tableView1;
@属性(弱、非原子)IBUITableView*tableView2;
@结束
.m:

#导入“ViewController.h”
#进口
@界面视图控制器()
@结束
@实现视图控制器
@综合名称场;
@综合公司领域;
@综合姓名;
@综合公司;
@综合现有名称;
@综合现有公司;
@合成tableView1=_tableView1;
@合成tableView2=_tableView2;
整数行数1;
整数行数2;
-(无效)viewDidLoad
{
[超级视图下载];
self.names=[[NSMutableArray alloc]init];
self.companys=[[NSMutableArray alloc]init];
self.existingNames=[[NSMutableArray alloc]init];
self.existingcompanys=[[NSMutableArray alloc]init];
//加载视图后,通常从nib执行任何其他设置。
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
如果(tableView==self.tableView1){
返回[现有名称计数];
}
else if(tableView==self.tableView 2){
返回[姓名计数];
}
返回0;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*simpleTableIdentifier=@“SimpleTableItem”;
UITableViewCell*单元格;
如果(tableView==self.tableView1){
cell=[\u tableView1 dequeueReusableCellWithIdentifier:simpleTableIdentifier];
}
else if(tableView==self.tableView 2){
cell=[\u tableView2 dequeueReusableCellWithIdentifier:simpleTableIdentifier];
}
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:simpleTableIdentifier];
}
如果(tableView==self.tableView1){
cell.textlab.text=[existingNames objectAtIndex:indexath.row];
}
else if(tableView==self.tableView 2){
cell.textlab.text=[names objectAtIndex:indexath.row];
}
返回单元;
}
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath{
如果(tableView==self.tableView1){
rowNumber1=indexPath.row;
}
else if(tableView==self.tableView 2){
rowNumber2=indexPath.row;
}
}
-(iAction)添加:(id)发件人{
BOOL exists=[existingNames containsObject:nameField.text];
if(exists==FALSE){
[names addObject:nameField.text];
[companys addObject:companyField.text];
[ExistingNamesAddObject:nameField.text];
[现有公司添加对象:companyField.text];
}
否则{
[names addObject:nameField.text];
[companys addObject:companyField.text];
}
[_tableView1重新加载数据];
[_tableView2重新加载数据];
nameField.text=@;
companyField.text=@;
}
-(iAction)addExisting:(id)发送方{
[existingNames addObject:[names objectAtIndex:rowNumber1]];
//[公司添加对象:[现有公司对象索引:行编号]];
[_tableView2重新加载数据];
}
-(iAction)删除:(id)发送方{
[existingNames removeObjectAtIndex:rowNumber2];
[现有公司删除objectatindex:rowNumber2];
[_tableView2重新加载数据];
}
@结束

以下方法是
UITableViewDataSource
UITableViewDelegate
协议的一部分

tableView:numberOfRowsInSection:
tableView:cellForRowAtIndexPath:
tableView:didSelectRowAtIndexPath:
并且将由您的表视图调用,前提是您已为表视图的
委托
数据源
属性分配了属性。但是这些方法,

tableView2:numberOfRowsInSection:
tableView2:cellForRowAtIndexPath:
tableView2:didSelectRowAtIndexPath:
不是协议的一部分,永远不会被表视图调用。看起来您可能混淆了属性名称(例如
tableView
)和协议方法名称(例如
tableView:numberofrowsinssection:
)。您需要做的是:

  • 如果尚未执行此操作,请将视图控制器设置为
    表视图
    表视图2
    委托
    数据源
  • 在需要实现的每个数据源和委托方法中,处理这两个表的情况
  • 例如,您的
    tableView:numberofrowsinssection:
    方法如下所示:

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        if (tableView == self.tableView) {
            return [existingNames count];
        }
    
        else if (tableView == self.tableView2) {
            return [names count];
        }
    
        return 0;
    }
    

    我已经为这两个表视图设置了数据源和委托。感谢您帮助理解tableview文档,它们真的很值得学习!我会努力的,如果有什么改进,我会告诉你的。我已经准备好了
    tableView2:numberOfRowsInSection:
    tableView2:cellForRowAtIndexPath:
    tableView2:didSelectRowAtIndexPath:
    
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        if (tableView == self.tableView) {
            return [existingNames count];
        }
    
        else if (tableView == self.tableView2) {
            return [names count];
        }
    
        return 0;
    }