UITableView单元格无法显示正确的细节

时间:2012-10-26 13:02:59

标签: ios uitableview uisearchbar

我创建了tableview和搜索栏。

单元格1的名称是iPhone。

当我点击手机iPhone时,在详细信息视图中它会写入iPhone。当我搜索iPod时,它成为搜索列表中的第一个单元格,但是当我点击它时,它会写入iPhone而不是iPod。我尝试将所有代码插入此处。

 - (BOOL)shouldAutootateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
    // Return YES for supported orientations
   return (interfaceOrientation == UIInterfaceOrientationPortrait);
      }

     #pragma  mark - Table View Methods


    - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{


    return 1;
  }
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section      {
  return [displayItems count];
       }


   - (UITableViewCell *)tableView:(UITableView *)aTableView       cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   static NSString *cellIdentifier = @"Cell";

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


cell.textLabel.text = [displayItems objectAtIndex:indexPath.row];


return cell;

 }

 -(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{

if ([searchText length] == 0) {
    [displayItems removeAllObjects];
    [displayItems addObjectsFromArray:allItems];

} else {

    [displayItems removeAllObjects];
    for (NSString * string in allItems ){
        NSRange r =[string rangeOfString:searchText options:NSCaseInsensitiveSearch];

        if (r.location != NSNotFound){
            [displayItems addObject:string];
        }
    }

    [tableView reloadData];

   } 

  }





   -(void)searchBarSearchButtonClicked:(UISearchBar *)asearchBar {

   [searchBar resignFirstResponder];



      }


    - (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellAccessoryDisclosureIndicator;
    }


       - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath



      {
   DetailViewController *DVC = [[DetailViewController alloc] init];

   //Pass the row to the DVC


DVC.characterNumber = [indexPath row];

//Push the DetailViewController object onto the stack

[self.navigationController pushViewController:DVC animated:YES];



 }
 @end

1 个答案:

答案 0 :(得分:1)

您要传递给allItems数组的详细控制器行号,但它与displayItems中的不一样

DVC.characterNumber = [indexPath row];

你应该从allItems数组中确定正确的行,而不是displayItems数组

<强>更新

NSString *item = [displayItems objectAtIndex:indexPath.row;
DVC.characterNumber = [allItems indexOfObject:item];