【问题标题】:iOS display data from array in table, except for last indexiOS在表格中显示数组中的数据,最后一个索引除外
【发布时间】:2013-03-04 02:37:40
【问题描述】:

我已经从一个数组的表格视图中显示了我的数据,但我不想显示数组中的最后一个对象。我该如何做到这一点?谢谢

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    MQManeuver *manuever = [[route maneuvers] objectAtIndex:indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    cell.imageView.image = nil;

    cell.detailTextLabel.numberOfLines = 2;
    cell.detailTextLabel.adjustsFontSizeToFitWidth = YES;
    cell.detailTextLabel.text = manuever.narrative;
    cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:12];
    cell.detailTextLabel.textColor = [UIColor blackColor];

    float distance = manuever.distance;
    NSString *distance1 = [NSString stringWithFormat:@"%.1f", distance];
    cell.textLabel.text = [distance1 stringByAppendingFormat:@" miles"];
    cell.textLabel.textColor = [UIColor grayColor];

    return cell;
}

【问题讨论】:

  • 在numberOfRowsInSection中,只返回[[route maneuvers] count] - 1;
  • 另一种选择是从数组中删除对象。

标签: ios uitableview nsarray directions


【解决方案1】:

试试这个

将 NSArray 数据转移到 NSMutableArray

NSMutableArray Methods for Removing Objects:
– removeAllObjects
– removeLastObject   ///you can use removeLastObject Method
– removeObject:
– removeObject:inRange:
– removeObjectAtIndex:
– removeObjectsAtIndexes:
– removeObjectIdenticalTo:
– removeObjectIdenticalTo:inRange:
– removeObjectsFromIndices:numIndices:
– removeObjectsInArray:
– removeObjectsInRange:

【讨论】:

    【解决方案2】:

    请尝试使用这个...我认为这会很好。

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        if([[route maneuvers] count] == 0)
            return 0;
        else
           return [[route maneuvers] count] - 1;
    }
    

    【讨论】:

    • 这有点危险,因为你可以返回一个 NSInteger。如果计数为 0,则返回 -1。这不是此方法的有效返回值(应该 >= 0... 或者更确切地说,返回类型应该是 NSUInteger)。
    • 是的老板谢谢你。我认为它的人的scence。所以他可以使用它。
    猜你喜欢
    • 2019-10-05
    • 2018-07-20
    • 2021-11-11
    • 2019-07-23
    • 2017-04-19
    • 2013-09-14
    • 2015-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多