简体   繁体   English

如何在UITableView中设置按钮的标题?

[英]How to Set the title of a button in UITableView?

HI I have a UITableView and I have data in my array, i have search functionality also here. 您好,我有一个UITableView并且数组中有数据,我在这里也有search功能。 this is a menu type thing. 这是菜单类型的东西。 There are different dishes and user can search any dish, on the left side here is a text Field and its text is 0, when user search some dish then type some other digit in it then how to save that input in the right place, Because at that time its its cell index will be different than original. 有不同的菜肴,用户可以搜索任何菜肴,左侧是一个文本字段,其文本为0,当用户搜索某个菜肴然后在其中键入其他数字时,如何将输入保存在正确的位置,因为届时其单元index将不同于原始index You can see this picture of my UITableView 您可以看到我的UITableView图片 在此处输入图片说明

I know this is a problem of logic but please help me I'm stuck here. 我知道这是逻辑上的问题,但请帮助我,我被困在这里。

请尝试以下操作:

[button setTitle:@"title" forState:UIControlStateNormal];

  1. At first take a NSMutableArray 首先,拿一个NSMutableArray

.h file .h文件

NSMutableArray *searchDataArray; NSMutableArray * searchDataArray;

Now declare its property and synthesize it in your .m file 现在声明其属性并将其合成到您的.m文件中

2.In your -(void)viewDidLoad alloc your search array and retain it. 2.在-(void)viewDidLoad分配搜索数组并保留它。

  1. [I am thinking that you can set searchbar and can call the search delegate]..so in [我认为您可以设置搜索栏并可以调用搜索代表]。

      - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{ [sBar resignFirstResponder]; searching = YES; //A boolean for searching condition [searchData removeAllObjects]; NSString *searchText = searchBar.text; // record is a mutable array containing the cell text int count = [records count]; for (int i=0; i<count; i++) { for(int j=0;j<6;j++){ //finding the cell strings for checking recordsstr = [NSString stringWithFormat:@"%@",[records objectAtIndex:i]; //Matching string of table and search string //case sensitivity checking NSString *mainString = [NSString stringWithFormat:@"%@",[recordsstr lowercaseString]]; NSString *searchString = [NSString stringWithFormat:@"%@",[searchText lowercaseString]]; NSLog(@"main String: %@ and search string: %@ ",mainString,searchString); NSLog(@"main string lenght: %d and search String lenght: %d",[mainString length],[searchString length]); if([mainString isEqualToString:searchString] || [mainString rangeOfString:searchString].location != NSNotFound){ [searchData addObject:[NSNumber numberWithInt:i]]; NSLog(@"indext after matching: %d",i); NSLog(@"number of date in searchdata array after matching: %d",[searchData count]); break; } recordsstr = nil; } } 

In this code i am matching the cell text and search text. 在此代码中,我将匹配单元格文本和搜索文本。 After finding the cell text taking array of cell index. 找到单元格文本后,获取单元格索引数组。

now when search is complete then reload your your tableview [tableview reloadData] 现在搜索完成后,请重新加载您的tableview [tableview reloadData]

Now in cellForRowAtIndex check that 现在在cellForRowAtIndex检查

if(searching == YES){
for(int i = 0; i<[searchData count]; i++){
  if(indexPath.row is equal to [searchData objectAtIndex:i])
   {
  //change your text field
   }
 }
 else{
  //Without searching code
   }

May be it will help you.If any problem knock me. 可能会对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM