你好,歡迎來到IOS教程網

 Ios教程網 >> IOS使用技巧 >> IOS技巧綜合 >> IOS之表視圖添加搜索欄

IOS之表視圖添加搜索欄

編輯:IOS技巧綜合
[摘要]本文是對IOS之表視圖添加搜索欄的講解,對學習IOS蘋果軟件開發有所幫助,與大家分享。

下面是我們要實現的效果。本效果是在上一篇自定義表視圖的基礎上進行更改的。

-(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;
}
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved