iphone - 我正在从服务器获取很多名称,但我只想在 UITableView 中显示 2 个名称

标签 iphone ios objective-c uitableview

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    { 
        return  [arrTemptemp count] ;
    }

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

        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if(!cell)
        {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
     for( int i =0; i<2  ; i++)
        {
          //  arrTemp = [[arrForum objectAtIndex:i] objectForKey:@"title"];
           // str1=[[arrForum objectAtIndex:i] objectForKey:@"title"];
            arrTemptemp = [[arrForum objectAtIndex:i] objectForKey:@"title"];
            NSLog(@"wwww:%@",arrTemptemp);
        }
        NSEnumerator *enumerator = [arrTemptemp objectEnumerator];
        NSDictionary *item = (NSDictionary*)[enumerator nextObject];

        NSLog(@"sheetal:%@",item);
        cell.textLabel.text = [NSString stringWithFormat:@"%@",item];
        return cell;
    }

最佳答案

第一次改变

 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  { 
      return  2 ;
  }

如果您想在 UITableView 上显示任意两个值,那么 这里证明了适用于 cellForRowAtIndexPath 的简单逻辑。

    if(indexPath.row == 0)
    {
        cell.textLabel.text = [[arrForum objectAtIndex:0] objectForKey:@"title"];
       // your can get whatever value from arrForum (YourArray) by change objectAtIndex
    }
    if(indexPath.row == 1)
    {
        cell.textLabel.text = [[arrForum objectAtIndex:1] objectForKey:@"title"];
       // your can get whatever value from arrForum (YourArray) by change objectAtIndex
    }

注意:我知道 cell.textLabel.text = [[arrForum objectAtIndex:indexPath.row] objectForKey:@"title"]; 就足够了,但它只得到数组的前两个值。所以我应用上面的逻辑,OP可以得到他想要的任意两个值。

关于iphone - 我正在从服务器获取很多名称,但我只想在 UITableView 中显示 2 个名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15086969/

相关文章:

iOS 11 - UserDefaults.standard 字符串值 nil

iphone - 如何从objective-c中的字符串中获取url子字符串

iphone - Flurry API 崩溃 iPhone 模拟器

iphone - 我可以将我当前的应用添加到应用内购买吗?

ios - 如何使用ffmpeg在ios中对h264直播数据进行解码和编码?

iphone - 在 UITableViewCell 中设置 textLabel 的宽度

ios - 设置 UISlider 的最小值和最大值时出错

ios - 为什么媒体选择器中的“取消”按钮不起作用?

iphone - 核心数据表到 NSArray

ios - 具有多种字体颜色的 UILabel 文本