简体   繁体   English

在数组中的UITableView中显示选择性数据

[英]Display selective data in UITableView from an array

I Have an NSMutableArray which is being displayed as 我有一个NSMutableArray显示为

 (
          13002,
          My Drive,
          13006,
          Testing1,
          13007,
          Testing123    
 )

In my NSLog 在我的NSLog中

I want to populate my UITableView with just the names (My Drive, Testing1 & Testing123) and want to use the ID's as the subtitle. 我想只用名称(“我的驱动器”,“ Testing1&Testing123”)填充UITableView,并希望使用ID作为字幕。

NSmutableArray *element; //contains your array

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

    static NSString *cellIdentifier = @"MyCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];

    element = [array objectAtIndex:indexPath.row];
    cell.textLabel.text = element;


    element = [array objectAtIndex:indexPath.row +1 ];
    cell.subtitle.text = element; 

use cell for row at indexpath method and number of rows method as below 在索引路径方法和行数方法中使用单元格作为行,如下所示

 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
        return array.count/2;
 }


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      int row = [indexPath row]*2;

      static NSString *CellIdentifier = @"Cell";

      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     if (cell == nil) {
              cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
              cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
              cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
     }    

     cell.textLabel.text = [array objectAtIndex:row+1];
     cell.detailTextLabel.text = [array objectAtIndex:row];

     return cell;
}

your problem will solve... 您的问题会解决...

Depending on your application, you might want to consider using a NSMutableDictionary instead of the array. 根据您的应用程序,您可能需要考虑使用NSMutableDictionary而不是数组。 This way, your data model will accurately represent the fact that those names are mapped to ids. 这样,您的数据模型将准确地表示这些名称已映射到ID的事实。 You can use 您可以使用

NSArray *arrayOf Keys = [theDictionary allKeys];

to get an array of the keys in your dictionary, then do something like this: 获取字典中键的数组,然后执行以下操作:

cell.textLabel.text = [theDictionary objectForKey:[arrayOfKeys objectAtIndex:indexPath.row]];
cell.detailtextLabel.text = [arrayOfKeys objectAtIndex:indexPath.row];

You should also consider using the free Sensible TableView framework. 您还应该考虑使用免费的Sensible TableView框架。 You would just pass the array to the framework and it will automatically display it in the table view. 您只需将数组传递给框架,它将自动在表视图中显示它。

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

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