在不影响动画的情况下,不能在didSelectRowAtIndexPath中调用cellForRowAtIndexPath?

时间:2016-06-30 07:24:37

标签: ios objective-c uitableview

我检查了所有重复的问题。但我没有找到解决方案。如果我的问题不正确,请纠正我。

我有自定义单元格标签(For Title)和textview(For Description)。我正在尝试可扩展的细胞。首先它只显示标题,当我点击时,它将显示描述。因此,在开始时,文本视图(描述)将被隐藏(我将在cellForRowAtIndexPath中指定)当用户触摸我在didSelectRowAtIndexPath函数中检测到的单元格时,我将使其可见我也会改变高度。

我知道我可以使用[tableView reload]函数来调用cellForRowAtIndexPath。但我正在使用

[tableView beginUpdates];
[tableView endUpdates];
当高度改变时,

didSelectRowAtIndexPath内为动画。在这里,如果我使用[tableView reload],动画无效。数据显示完美,但没有动画。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView beginUpdates];
    if ([indexPath compare:self.expandedIndexPath] == NSOrderedSame) {
        self.expandedIndexPath = nil;
    } else {
        self.expandedIndexPath = indexPath;
    }
  [tableView endUpdates];
}

我想在cellForRowAtIndexPath内调用didSelectRowAtIndexPath函数而不影响动画。

感谢您的时间(:

我的代码:

#import "MyListViewController.h"
#import "CustomCell.h"

@interface MyListViewController ()
@property NSArray* tableData;
@property (strong, nonatomic) NSIndexPath *expandedIndexPath;
@end

@implementation MyListViewController

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

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

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.secondary.text = @"adfasdfasdfsdfsdffassssssddalfdsaldjsfadsfasdfdjsfldsjfakldsfjlasfaksfjadksfaldfjladksfjadksfjdkslsdjjlkflasfaljfajfalkflkadslsdfljksdfajsdasdfjlkafsjlkasdjfafsdjadfjksadjsladfjsajladfjslas";
    if ([indexPath compare:self.expandedIndexPath] == NSOrderedSame) {
        cell.primaryLabel.text = [self.tableData objectAtIndex:indexPath.row];
        cell.secondary.hidden = NO;

    }else{
        cell.primaryLabel.text = [self.tableData objectAtIndex:indexPath.row];
        cell.secondary.hidden = YES;
    }
       // cell.textLabel.text = [self.tableData objectAtIndex:indexPath.row];
   // cell.imageView.image = [UIImage imageNamed:@"creme_brelee.jpg"];
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    //[tableView reloadData];
    if ([indexPath compare:self.expandedIndexPath] == NSOrderedSame) {

        return 100.0; // Expanded height

    }
    return 30.0; // Normal height
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView beginUpdates];
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    if ([indexPath compare:self.expandedIndexPath] == NSOrderedSame) {
        self.expandedIndexPath = nil;
    } else {
        self.expandedIndexPath = indexPath;
    }
  [tableView endUpdates];
}



- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.delegate=self;
    self.tableView.dataSource=self;

    self.tableData = [NSArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", @"Full Breakfast", @"Hamburger", @"Ham and Egg Sandwich", @"Creme Brelee", @"White Chocolate Donut", @"Starbucks Coffee", @"Vegetable Curry", @"Instant Noodle with Egg", @"Noodle with BBQ Pork", @"Japanese Noodle with Pork", @"Green Tea", @"Thai Shrimp Cake", @"Angry Birds Cake", @"Ham and Cheese Panini", nil];


    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
 }




@end

2 个答案:

答案 0 :(得分:2)

[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationAutomatic];//indexpath of your selected row
[tableView endUpdates]; 

答案 1 :(得分:1)

你应该这样做并根据你的要求改变动画

[
  {
    count: "4",
    date: "2015-09-23",
    district: 12,
    to_timestamp: "2015-09-24T00:00:00.000Z",
    type: "MOTOR VEHICLE THEFT"
  },
  {
    count: "13",
    date: "2015-09-23",
    district: 12,
    to_timestamp: "2015-09-24T00:00:00.000Z",
    type: "BURGLARY"
  },
  ...
  {
    count: "3",
    date: "2016-03-23",
    district: 12,
    to_timestamp: "2016-03-24T00:00:00.000Z",
    type: "BURGLARY"
  }
]