如何为每个UITableView部分设置Bool值不同

时间:2014-10-15 18:21:49

标签: ios objective-c uitableview

我想在这里做的是设置它,以便在top3ArrayForSection.count-1 < 1时,将相应部分_results bool值设置为NO,依此类推。发生了什么,是整个表整体_results设置为NO或YES,所以我得到这样的结果:

enter image description here

当只有“xperia Z3 compact unlocked”部分应该说“没有找到物品等”时因为它没有细胞,其他部分细胞不应该。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSDictionary *currentSectionDictionary = _matchCenterArray[section];
    NSArray *top3ArrayForSection = currentSectionDictionary[@"Top 3"];

    if (top3ArrayForSection.count-1 < 1){
        _results = NO;
        _rowCount = 1;
    }
    else if(top3ArrayForSection.count-1 >= 1){
        _results = YES;
        _rowCount = top3ArrayForSection.count-1;
    }

    return _rowCount;
}


// Cell layout
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Initialize cell
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        // if no cell could be dequeued create a new one
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    // No cell seperators = clean design
    tableView.separatorColor = [UIColor clearColor];

    if (_results == NO) {

        // title of the item
        cell.textLabel.text = @"No items found, but we'll keep a lookout for you!";
        cell.textLabel.font = [UIFont boldSystemFontOfSize:12];

    }

    else if (_results == YES) {

        // title of the item
        cell.textLabel.text = _matchCenterArray[indexPath.section][@"Top 3"][indexPath.row+1][@"Title"];
        cell.textLabel.font = [UIFont boldSystemFontOfSize:14];

        // price of the item
        cell.detailTextLabel.text = [NSString stringWithFormat:@"$%@", _matchCenterArray[indexPath.section][@"Top 3"][indexPath.row+1][@"Price"]];
        cell.detailTextLabel.textColor = [UIColor colorWithRed:0/255.0f green:127/255.0f blue:31/255.0f alpha:1.0f];

        // image of the item
        NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:_matchCenterArray[indexPath.section][@"Top 3"][indexPath.row+1][@"Image URL"]]];
        [[cell imageView] setImage:[UIImage imageWithData:imageData]];

    }

    return cell;

}

1 个答案:

答案 0 :(得分:1)

您的实例变量_results是一维的。您可以将其替换为NSArray并单独存储值,或者您可以更改代码中的逻辑:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  NSDictionary *currentSectionDictionary = _matchCenterArray[section];
  NSArray *top3ArrayForSection = currentSectionDictionary[@"Top 3"];

  return (top3ArrayForSection.count-1 < 1) ? 1 : top3ArrayForSection.count-1;
}


// Cell layout
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  // Initialize cell
  static NSString *CellIdentifier = @"Cell";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (!cell) {
    // if no cell could be dequeued create a new one
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  }

  // No cell seperators = clean design
  tableView.separatorColor = [UIColor clearColor];

  NSDictionary *currentSectionDictionary = _matchCenterArray[indexPath.section];
  NSArray *top3ArrayForSection = currentSectionDictionary[@"Top 3"];

  if (top3ArrayForSection.count-1 < 1) {

    // title of the item
    cell.textLabel.text = @"No items found, but we'll keep a lookout for you!";
    cell.textLabel.font = [UIFont boldSystemFontOfSize:12];

  }

  else {

    // title of the item
    cell.textLabel.text = _matchCenterArray[indexPath.section][@"Top 3"][indexPath.row+1][@"Title"];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:14];

    // price of the item
    cell.detailTextLabel.text = [NSString stringWithFormat:@"$%@", _matchCenterArray[indexPath.section][@"Top 3"][indexPath.row+1][@"Price"]];
    cell.detailTextLabel.textColor = [UIColor colorWithRed:0/255.0f green:127/255.0f blue:31/255.0f alpha:1.0f];

    // image of the item
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:_matchCenterArray[indexPath.section][@"Top 3"][indexPath.row+1][@"Image URL"]]];
    [[cell imageView] setImage:[UIImage imageWithData:imageData]];

  }

  return cell;

}