草庐IT

iphone - xib 中带有 customcell 的 UItableview 和 searchbar

coder 2024-01-17 原文

我是 Iphone 应用程序开发的新手。我需要开发一个带有 UISearchbarUITableview。我必须在 tableView 中显示一个 json 数组,默​​认为一个 image 和两个 buttons 和两个 标签。两个按钮的功能是 + 和 - 一个标签用于显示按钮操作,一个标签用于显示 json 数组名称的数字。

确切地说,我需要显示的是...在特定单元格的标签中单击按钮的次数,并在搜索栏中搜索相同项目时显示相同的次数。

谁能帮帮我。请....

提前致谢。

这是我试过的代码..

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

{

    static NSString *CellIdentifier = @"ListOfProductsCell";

    ListOfProductsCell *cell = (ListOfProductsCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell==nil) {
            NSArray *nib =[[NSBundle mainBundle] loadNibNamed:@"ListOfProductsCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];

        productItemDit=[productListArry objectAtIndex:indexPath.row];
        NSString *offerStr= [NSString stringWithFormat:@"%.2f",[[productItemDit objectForKey:@"offer"] floatValue]];
        NSString *fullCostStr=[[currencyCodeStr stringByAppendingString:@" "] stringByAppendingString:offerStr];
        NSLog(@"%@",fullCostStr);
        cell.itemCostLbl.text=fullCostStr;

        cell.itemStepper.tag=166;
        cell.itemAddedLbl.tag=122;

        itemCount=stepperValueArry.count;
        cell.itemAddedLbl =(UILabel*)[cell viewWithTag:122];
        cell.itemAddedLbl.text = [[NSString alloc] initWithFormat:@"%d",itemCount];

    }

    cell.itemImg.image = [UIImage imageNamed:@"profp.jpg"];

    if (tableView == self.searchDisplayProduct.searchResultsTableView) {
        searchProductItemDit=[searchProductListArry objectAtIndex:indexPath.row];
        NSLog(@"searchdit:%@",searchProductItemDit);
        cell.itemNameLbl.text= [searchProductItemDit objectForKey:@"name"];
        self.searchDisplayProduct.searchResultsTableView.separatorColor=[UIColor colorWithRed:200.0 green:0.0 blue:0.0 alpha:1.0];
    } else {
        productItemDit=[productListArry objectAtIndex:indexPath.row];
        NSLog(@"dit:%@",productItemDit);
        cell.itemNameLbl.text=[productItemDit objectForKey:@"name"];

    }

    return cell;

}

最佳答案

您可以将 json 中的数据放入 dataSource1 数组中,并像下面的代码一样实现搜索栏。 谷歌搜索:- Json Parsing,阅读 UITableView 和 SearchBar 的文档以自定义以下代码

在ViewController.h中

@interface ViewController : UIViewController<UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate>{

UITableView *myTableView;
NSMutableArray *dataSource1; //will be storing all the data
NSMutableArray *tableData;//will be storing data that will be displayed in table
NSMutableArray *searchedData;//will be storing data matching with the search string
UISearchBar *sBar;//search bar
}
@property(nonatomic,retain)NSMutableArray *dataSource1;

在 ViewController.m 中

 - (void)viewDidLoad
    {
        [super viewDidLoad];
        sBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0,0,320,40)];
        sBar.delegate = self;
        [self.view addSubview:sBar];

        myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 41, 320, 400)];
        myTableView.delegate = self;
        myTableView.dataSource = self;
        [self.view addSubview:myTableView];

        //initialize the two arrays; dataSource will be initialized and populated by appDelegate
        searchedData = [[NSMutableArray alloc]init];
        tableData = [[NSMutableArray alloc]init];
        dataSource1 = [[NSMutableArray alloc]init];
        [dataSource1 addObject:@"vivk"];
        [dataSource1 addObject:@"Balveer"];
        [dataSource1 addObject:@"Balveer"];
        [dataSource1 addObject:@"dharmender"];
        [dataSource1 addObject:@"aaaaa"];
        [dataSource1 addObject:@"aabb"];
        [dataSource1 addObject:@"bbbb"];
        [dataSource1 addObject:@"ba"];
        [dataSource1 addObject:@"ankit"];
        [dataSource1 addObject:@"cccc"];
        [dataSource1 addObject:@"ddddd"];


        [tableData addObjectsFromArray:dataSource1];//on launch it should display all the records
    }

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 1;
    }
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        //NSLog(@"contacts error in num of row");
        return [tableData count];
    }
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }
        cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
        return cell;
    }
    #pragma mark UISearchBarDelegate
    - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
    {
        // only show the status bar's cancel button while in edit mode
        sBar.showsCancelButton = YES;
        sBar.autocorrectionType = UITextAutocorrectionTypeNo;
        // flush the previous search content
        [tableData removeAllObjects];
    }
    - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
    {
        sBar.showsCancelButton = NO;
    }

    - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
    {
        [tableData removeAllObjects];// remove all data that belongs to previous search
        if([searchText isEqualToString:@""]||searchText==nil){
            [myTableView reloadData];
            return;
        }
        NSInteger counter = 0;
        for(NSString *name in dataSource1)
        {
            NSRange r = [name rangeOfString:searchText options:NSCaseInsensitiveSearch];
            if(r.location != NSNotFound)
                [tableData addObject:name];
            counter++;
        }
        [myTableView reloadData];
    }
    - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
    {
        // if a valid search was entered but the user wanted to cancel, bring back the main list content
        [tableData removeAllObjects];
        [tableData addObjectsFromArray:dataSource1];
        @try{
            [myTableView reloadData];
        }
        @catch(NSException *e){
        }
        [sBar resignFirstResponder];
        sBar.text = @"";
    }
    // called when Search (in our case "Done") button pressed
    - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
    {
        [searchBar resignFirstResponder];
    }
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

关于iphone - xib 中带有 customcell 的 UItableview 和 searchbar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17289722/

有关iphone - xib 中带有 customcell 的 UItableview 和 searchbar的更多相关文章

  1. ruby - ruby 中带有多行字符串的空格 - 2

    我有一个多行字符串的空白问题。我在生成一些SQL的代码中有类似的内容。defgenerate_sql但是我的SQL缩进全乱了,这是我不想要的。"UPDATEpage\nSETview_count=10;\n"我能做到defgenerate_sql输出的正是我想要的"UPDATEpage\nSETview_count=10;\n"但是我的代码缩进全乱了,我真的不想要这样。关于如何最好地实现我所追求的目标有什么建议吗? 最佳答案 Ruby2.3.0使用squigglyheredoc很好地解决了这个问题.请注意示例之间波浪号/连字符的区别

  2. ruby-on-rails - Rails 中带后备功能的动态命名空间 Controller - 2

    我对新的Rails应用程序有一个有点奇怪的要求。我需要构建一个应用程序,其中所有路由都在多个命名空间中定义(让我解释一下)。我想要一个应用程序,其中学校科目(数学、英语等)是namespace:%w[mathenglish].eachdo|subject|namespacesubject.to_symdoresources:studentsendend这很棒而且有效,但它需要我为每个主题创建一个命名空间StudentsController,这意味着如果我添加一个新主题,那么我需要创建一个新Controller。我想创建一个Base::StudentsController,如果Math:

  3. iphone - 扩展 restful_authentication/AuthLogic 以支持匿名 iPhone 的延迟登录的最佳方法是什么? - 2

    我正在构建一个与RubyonRails后端对话的iPhone应用程序。RubyonRails应用程序还将为Web用户提供服务。restful_authentication插件是提供快速和可定制的用户身份验证的绝佳方式。但是,我希望iPhone应用程序的用户在新列中存储一个由手机的唯一标识符([[UIDevicedevice]uniqueIdentifier])自动创建的帐户。稍后,当用户准备好创建用户名/密码时,帐户将更新为包含用户名和密码,iPhone唯一标识符保持不变。用户在设置用户名/密码之前不能访问该网站。然而,他们可以使用iPhone应用程序,因为该应用程序可以使用它的标识符

  4. iphone - 设计和 Rails 3 中的 http 身份验证 - 2

    我有一个使用deviseonrails3的应用程序。我想启用http身份验证,以便我可以从iPhone应用程序向我的网络应用程序进行身份验证。如何从我的iPhone应用程序进行身份验证以进行设计?这安全吗?还是我应该进行不同的身份验证? 最佳答案 从设计的角度来看,您有3个选择:1)使用基本的http身份验证:您的iPhone应用程序有一个secretkey-这是在您的iPhone应用程序代码中烘焙的-用于对网络应用程序的每个请求进行身份验证。Google搜索:“设计基本的http身份验证”2)您可以通过在您的iPhone应用程序中

  5. ruby - ruby 中带哈希的字符串插值 - 2

    我的目标是用散列中的值替换字符串中的键。我是这样做的:"hello%{name},todayis%{day}"%{name:"Tim",day:"Monday"}如果字符串中的键在散列中丢失:"hello%{name},todayis%{day}"%{name:"Tim",city:"Lahore"}然后它会抛出一个错误。KeyError:key{day}notfound预期结果应该是:"helloTim,todayis%{day}"or"helloTim,todayis"有人可以指导我只替换匹配的键而不抛出任何错误吗? 最佳答案

  6. ruby-on-rails - Rails 中带有 text_field 的逗号分隔数组 - 2

    我有一些用户可以有很多帖子,并且每个帖子都可以有很多标签。我已经使用帖子和标签之间的has_and_belongs_to_many关系实现了这一点。创建新帖子时,用户可以使用逗号分隔的值列表对其进行标记(很像在SO上发布新问题时)。如果任何标签尚不存在,则应自动创建。这是帖子的_fields.html.erb部分内容:f.object%>现在使用f.text_field:tags会生成带有[]文本的输入元素。我还没有在posts_controller.rb中使用标签,因为我不确定我应该如何从参数中获取和拆分字符串值:defcreate@post=current_user.posts.b

  7. iphone - 在没有 Mac 的情况下开发 iPhone 应用程序? - 2

    这个问题在这里已经有了答案:关闭13年前。PossibleDuplicates:HowcanIdevelopforiPhoneusingaWindowsdevelopmentmachine?我想为我妻子的手机构建一个iPhone应用程序,但我对购买Mac作为一次性工作的开发平台不感兴趣。应用程序:应该在iPhone上独立运行(即没有网络连接)完全可以接受使用iPhoneJavascript库之一创建的GUI会做一些数据库IO来读取和更新数据没有商业值(value),永远不会被任何人使用这是我的想法:越狱iPhone在iPhone上安装Ruby+Sinatra使用Sinatra编写应用程

  8. iphone - iPhone 原生应用的测试驱动设计 - 2

    我正在试验iPhoneSDK并在Nic博士的rbiPhoneTest项目中做一些TDD。我想知道有多少人(如果有的话)成功地使用了这个或任何其他iPhone/Cocoa测试框架?更重要的是,我想知道如何最好地断言专有的二进制请求/响应协议(protocol)。这个想法是通过网络发送二进制请求并接收二进制响应。请求和响应是使用byteand'ing和or'ing创建的。我正在使用黄金副本模式来测试我的请求。这是我到目前为止所拥有的。不要笑,因为我是ObjectiveC和Ruby的新手:requireFile.dirname(__FILE__)+'/test_helper'require'

  9. iphone - 如何从视频中提取方向信息? - 2

    在网络上浏览了大量文档后,iPhone似乎总是以480x360的纵横比拍摄视频,并在视频rails上应用变换矩阵。(480x360可能会改变,但对于给定设备而言始终相同)这是一种在iOS项目中修改ffmpeg源代码并访问矩阵http://www.seqoy.com/correct-orientation-for-iphone-recorded-movies-with-ffmpeg/的方法这是在iOS-4中查找转换矩阵的更清晰的方法Howtodetect(iPhoneSDK)ifavideofilewasrecordedinportraitorientation,orlandscape.

  10. Ruby 1.9 哈希在键中带有破折号 - 2

    在ruby​​1.9中有没有办法用新语法定义这个散列?irb>{a:2}=>{:a=>2}irb>{a-b:2}SyntaxError:(irb):5:syntaxerror,unexpectedtLABEL{a-b:2}^用旧的,它在工作:irb>{:"a-b"=>2}=>{:"a-b"=>2} 最佳答案 有些合法符号不能与新语法一起使用。我找不到引用,但似乎允许与/^[a-zA-Z_][a-zA-Z_0-9]*[!?]?$/匹配的符号名称新语法。最后一个字符可能是特殊字符“!”或“?”。对于任何不符合这些限制的符号,您必须使用R

随机推荐