2009-07-06 51 views
-1

如果我创建了一个部分,则会发生错误。UITableview部分错误 - objc_msgSend

我已经尝试将节索引设置为0和1,但这也没有帮助。

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

    static NSString *CellIdentifier = @"Cell"; 
    static NSString *CellIdentifier1 = @"Cell1"; 


    if(indexPath.section == 1) { 
     if(indexPath.row == 0) { 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     cell.text = @"test 1"; 

     } 

     return cell; 
    } 
    else if(indexPath.row == 1) { 
     UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; 
     if (cell1 == nil) { 
      cell1 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease]; 
      cell1.text = @"test 2"; 

     } 

     return cell1; 
    } 
    } 

else if(indexPath.section == 2) { 
    if(indexPath.row == 0) { 

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
      cell.text = @"test 1"; 

     } 

     return cell; 
    } 
    else if(indexPath.row == 1) { 
UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; 
     if (cell1 == nil) { 
      cell1 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease]; 
      cell1.text = @"test 2"; 

     } 

     return cell1; 
    } 
} 
} 

回答

0

节和行从0开始,据我所看到的,你不返回indexPath细胞第0,0行

编辑:重新发布源代码(更可读):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 
    static NSString *CellIdentifier1 = @"Cell1"; 
    if(indexPath.section == 1) { 
     if(indexPath.row == 0) { 
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
      if (cell == nil) { 
       cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
       cell.text = @"test 1"; 
      } 
      return cell; 
     } 
     else if(indexPath.row == 1) { 
      UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; 
      if (cell1 == nil) { 
       cell1 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease]; 
       cell1.text = @"test 2"; 
      } 
      return cell1; 
     } 
    } 
    else if(indexPath.section == 2) { 
     if(indexPath.row == 0) { 
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
      if (cell == nil) { 
       cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
       cell.text = @"test 1"; 
      } 
      return cell; 
     } 
     else if(indexPath.row == 1) { 
      UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; 
      if (cell1 == nil) { 
       cell1 = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1] autorelease]; 
       cell1.text = @"test 2"; 
      } 
      return cell1; 
     } 
    } 
} 
+0

节从1开始实际。函数 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView`应该总是返回至少'1'而不是'0'..这也可能导致崩溃。 – Jake 2009-07-06 10:05:29

0

基本问题。你有更新 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)部分的节数。 正常obj味精发送错误..当你正在处理你已经发布的东西或你不拥有的东西时例如... 在表视图的init方法中你写这个代码 sectionArray = [NSArray arraywithcontentsoffile:xyz.plist]

并在numberofRowsInSection中使用类似这样的东西 [sectionarray count];

其中sectionArray是一个实例变量..

-1

感谢您的回复!

我已经写了整个代码一次,并运行它,每次我添加一行代码。这有帮助。 我认为问题在于if(indexPath.section == 0)等等。 我只设置了3个部分中的2个部分的内容。

但我不确定这是否是问题。

感谢您的大力帮助!

作为一个初学者目标c是没那么容易:)