下面是我們要實現的效果。本效果是在上一篇自定義表視圖的基礎上進行更改的。
-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath { staticNSString*CellIdentifier=@"myTableCell"; MyTableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:CellIdentifier]; //addcodebegin:important,forshowingsearchingresults //不對cell進行空值的判斷,會導致在搜索時,找不到對應identifier的cell而報錯。 if(cell==nil){ //搜索結果采用簡單表視圖cell的style,並非自定義的表視圖cell的style cell=[[MyTableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:CellIdentifier]; NSUIntegerrow=[indexPathrow]; NSDictionary*rowDict=[dataArrobjectAtIndex:row]; cell.textLabel.text=[rowDictobjectForKey:@"itemName"]; NSString*imagePath=[rowDictobjectForKey:@"itemImagePath"]; cell.imageView.image=[UIImageimageNamed:imagePath]; } //addcodeend NSUIntegerrow=[indexPathrow]; NSDictionary*rowDict=[dataArrobjectAtIndex:row]; cell.label.text=[rowDictobjectForKey:@"itemName"]; NSLog(@"cell.label.text=%@",[rowDictobjectForKey:@"itemName"]); NSString*imagePath=[rowDictobjectForKey:@"itemImagePath"]; cell.image.image=[UIImageimageNamed:imagePath]; NSLog(@"cell.image.image=%@",imagePath); cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; returncell; }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"myTableCell"; MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //add code begin:important,for showing searching results //不對cell進行空值的判斷,會導致在搜索時,找不到對應identifier的cell而報錯。 if (cell == nil) { //搜索結果采用簡單表視圖cell的style,並非自定義的表視圖cell的style cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; NSUInteger row = [indexPath row]; NSDictionary *rowDict = [dataArr objectAtIndex:row]; cell.textLabel.text = [rowDict objectForKey:@"itemName"]; NSString *imagePath = [rowDict objectForKey:@"itemImagePath"]; cell.imageView.image = [UIImage imageNamed:imagePath]; } //add code end NSUInteger row = [indexPath row]; NSDictionary *rowDict = [dataArr objectAtIndex:row]; cell.label.text = [rowDict objectForKey:@"itemName"]; NSLog(@"cell.label.text = %@",[rowDict objectForKey:@"itemName"]); NSString *imagePath = [rowDict objectForKey:@"itemImagePath"]; cell.image.image = [UIImage imageNamed:imagePath]; NSLog(@"cell.image.image = %@",imagePath); cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; return cell; }