【问题标题】:iphone:Display JSON data in uitableviewcelliphone:在uitableviewcell中显示JSON数据
【发布时间】:2012-02-10 03:44:22
【问题描述】:

我使用了以下代码:

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


    static NSString *CellIdentifier = @"Cell";



    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
      //  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
       cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 

    }    
if (indexPath.row == 0) {

            CGRect myImageRect = CGRectMake(120.0f, 5.0f, 70.0f, 55.0f);
            UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
            [myImage setImage:[UIImage imageNamed:@"logo.png"]];
            [cell.contentView addSubview:myImage];
        }
        else  {

            int arrayIndex = [indexPath row]-1 ;
            mdict = [mmenuitems objectAtIndex:arrayIndex];
            [mdict retain];
            cell.textLabel.text =[mdict objectForKey:@"name"];

我在 mmenuitems 中得到了正确的 JSON 解析消息,并使用 indexPath.row = 0 来显示徽标。
但问题是我没有得到 tableview 中的最后一项。
此外,当我滚动 tableview 时,数据会随机重新加载,并且会重复。

【问题讨论】:

    标签: iphone objective-c ios json uitableview


    【解决方案1】:

    我可以猜测您需要将 1 添加到您的行数方法:

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
         return [mmenuitems count]+1//add 1 for the logo cell
     }
    

    因此,您的行数将是 mmenuitems 数组中的所有对象 + 1 个用于标识的单元格。

    但是

    更好的方法是将您的徽标添加到表格视图header view 而不是表格视图行,您可以在加载表格视图后在您的视图中执行此操作:

            CGRect myImageRect = CGRectMake(120.0f, 5.0f, 70.0f, 55.0f);
            UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
            [myImage setImage:[UIImage imageNamed:@"logo.png"]];
            [self.tableView.tableHeaderView addSubview:myImage];
    

    它会将您的徽标添加到第一个表格行顶部的视图中,并且不会混合在您的表格行中。我相信它会解决你的重复问题。

    祝你好运

    【讨论】:

    • 感谢您的回答。当我按照您所说的将徽标放在 tableview 上方时,但无法向上滚动。是否可以使用 tableview 向上滚动。
    • 你能解释一下你不能滚动是什么意思吗?
    • 我的 headerview 锚定在导航栏上。我希望它也可以使用 tableview 向上滚动
    【解决方案2】:

    您可以在 numberOfRowsInSection 方法中添加 + 1。

    【讨论】:

    • 感谢它帮助显示了整个数据。但仍然存在随机重新加载的问题。
    • 你能发布你的 mmenuitems 的 NSLog 吗?
    【解决方案3】:

    您可以尝试对单元格使用不同的标识符。因为单元格正在被重用,所以您的内容被覆盖了。这不是好方法,但它仍然解决了问题。

    【讨论】:

      【解决方案4】:

      首先,您需要每次都重置您的单元格以避免过时的数据

      if (cell == nil) {
        //  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
      
      }  
      cell.textLabel.text = nil; //This clears any stale label
      for(UIImageView *view in cell.contentView.subviews)
      {
          [view removeFromSuperView]; // This clears any subview added
      }  
      

      其次,

      你的行数应该是

       [mmenuitems count] + 1
      

      我相信目前只是 [mmenuitems count]

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-10
        • 2013-07-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多