使用单元格属性滚动到UITableView中的特定单元格

时间:2016-09-06 09:04:43

标签: ios objective-c uitableview nsindexpath

我有一个UITableViewCell示例如下:

//
//  ItemTableViewCell.h
//
#import <UIKit/UIKit.h>

@interface ItemTableViewCell : UITableViewCell

@property (weak, nonatomic) NSString *itemId;

@end

在我的UIViewController UITableView我有以下分配:

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

    NSManagedObject *row = [self.itemsFRC objectAtIndexPath:indexPath];
    ItemTableViewCell *cell = (ItemTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:@"cellId" forIndexPath:indexPath];

    cell.itemId = [row valueForKey:@"item_id"];
}

现在,如何使用单元格属性UITableView滚动到itemId中的特定单元格?

注意:我更倾向于不使用indexPathForRow方法。

1 个答案:

答案 0 :(得分:1)

您应该执行以下操作:

  1. NSManagedObject
  2. 查找item_id
  3. indexPath
  4. 获取找到的对象NSFetchedResultsController
  5. tableView滚动到indexPath
  6. 请参阅下面的代码示例:

    - (void) scrollToRowWithItemID: (NSString *) itemID {
        NSArray *fetchedObjects = self.itemsFRC.fetchedObjects;
        NSPredicate *predicate = [NSPredicate predicateWithFormat: @"item_id == %@", itemID];
        NSArray *filteredArray = [fetchedObjects filteredArrayUsingPredicate: predicate];
        NSIndexPath *indexPath = [self.itemsFRC indexPathForObject: filteredArray.firstObject];
        [self.tableView scrollToRowAtIndexPath: indexPath atScrollPosition: UITableViewScrollPositionNone animated: true];   
    }