tableview 및 검색 창을 만들었습니다.UITableView 셀에 정확한 세부 정보가 표시되지 않습니다.
셀 1의 이름은 iPhone입니다.
셀 iPhone을 클릭하면 상세보기에서 iPhone에 씁니다. iPod을 검색하면 검색 목록의 첫 번째 셀이되지만이를 클릭하면 iPod 대신 iPhone이 기록됩니다. 여기에 모든 코드를 삽입 해 봅니다.
- (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
우리는 필요 조금 더 자세히. didSelectRowAtIndexPath에서 사용중인 코드를 게시하고 Storyboards를 사용하는 경우 구현 한 prepareForSegue 메서드가 필요합니다. –
코드로 주제를 편집했습니다 – ffds