1

我必须为每个UITableViewCell. 随着时间的推移,必须删除那个cell. 我已经开始实施,但坚持如何删除那个特定cell的时间已经完成。我的应用程序有多行,但出于演示目的,我只显示了三行。

我的方法是:在TableViewController.m

#import "TableViewController.h"
#import "CountdownTimer.h"

@interface TableViewController () <CountdownTimerDelegate>
@property (nonatomic, strong) NSArray *demoList;
@end

@implementation TableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.demoList = [self dataSource];
}

- (NSArray *)dataSource {
    NSDictionary *demoOne = @{@"DemoId": [NSNumber numberWithInt:1],
                                 @"DemoName": @"Timer One",
                                 @"DueDate": [[NSDate date] dateByAddingTimeInterval:60]};

    NSDictionary *demoTwo = @{@"DemoId": [NSNumber numberWithInt:2],
                                 @"DemoName": @"Timer Two",
                                 @"DueDate": [[NSDate date] dateByAddingTimeInterval:120]};

    NSDictionary *demoThree = @{@"DemoId": [NSNumber numberWithInt:3],
                                   @"DemoName": @"Timer Theree",
                                   @"DueDate": [[NSDate date] dateByAddingTimeInterval:180]};

    return @[demoOne, demoTwo, demoThree];
}

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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.demoList.count;
}

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

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

    cell.textLabel.text = [[self.demoList objectAtIndex:indexPath.row] objectForKey:@"DemoName"];

    NSDate *dueDate = [[self.demoList objectAtIndex:indexPath.row] objectForKey:@"DueDate"];
    NSNumber *demoId = [[self.demoList objectAtIndex:indexPath.row] objectForKey:@"DemoId"];

    CountdownTimer *timer = [[CountdownTimer alloc] init];
    timer.delegate = self;
    [timer startCountdownTimerFordemo:demoId endDate:dueDate withUpdatingLable:cell.detailTextLabel];

    return cell;
}

#pragma mark - CountdownTimerDelegate

- (void)countdownTimer:(CountdownTimer *)ct didFinishTimerFordemo:(NSNumber *)demoId
{
    // Here I have to delete row having this demoId.
}

In CountdownTimer.h
@protocol CountdownTimerDelegate;

@interface CountdownTimer : NSObject

@property (assign) id <CountdownTimerDelegate> delegate;
- (void)startCountdownTimerFordemo:(NSNumber *)dId endDate:(NSDate *)date withUpdatingLable:(UILabel *)label;
@end

@protocol CountdownTimerDelegate <NSObject>
- (void)countdownTimer:(CountdownTimer *)ct didFinishTimerFordemo:(NSNumber *)demoId;
@end

In CountdownTimer.m
@interface CountdownTimer () {
    NSDate *dueDate;
    NSNumber *demoId;
}

// The repeating timer is a assign/weak property.
@property (assign) NSTimer *countdownTimer;
@property (retain, nonatomic) UILabel *updatingLabel;

@end

@implementation CountdownTimer

- (void)startCountdownTimerFordemo:(NSNumber *)dId endDate:(NSDate *)date withUpdatingLable:(UILabel *)label
{
    self.updatingLabel = label;
    demoId = dId;
    dueDate = date;

    // Cancel a preexisting timer.
    [self.countdownTimer invalidate];

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1
                                                      target:self selector:@selector(updateRemainingTime:)
                                                    userInfo:nil repeats:YES];

    self.countdownTimer = timer;
}

- (void)updateRemainingTime:(NSTimer*)theTimer
{
    int interval = (int)[dueDate timeIntervalSinceDate:[NSDate date]];

    int minutes = floor(interval/60);
    int seconds = round(interval - minutes * 60);

    if ((minutes > 0 || seconds >= 0) && minutes >= 0) {
        if (minutes >= 0)
            [self.updatingLabel setText:[NSString stringWithFormat:@"%d:%02d", minutes, seconds]];

        if (seconds == 0) {
            seconds = 59;
            minutes = minutes - 1;
        } else {
            seconds = seconds - 1;
        }
    } else {
        [self.countdownTimer invalidate];
        [self.delegate countdownTimer:self didFinishTimerFordemo:demoId];
    }
}
4

1 回答 1

0

你可以先比较数组中存在的每个对象你必须使用可变数组听是代码


    @property (nonatomic, retain) NSMutableArray *demoList; //change to NSMutableArray

    - (NSMutableArray *)dataSource {
   NSDictionary *demoOne = @{@"DemoId": [NSNumber numberWithInt:1],
   @"DemoName": @"Timer One",
   @"DueDate": [[NSDate date] dateByAddingTimeInterval:60]};

   NSDictionary *demoTwo = @{@"DemoId": [NSNumber numberWithInt:2],
   @"DemoName": @"Timer Two",
   @"DueDate": [[NSDate date] dateByAddingTimeInterval:120]};

   NSDictionary *demoThree = @{@"DemoId": [NSNumber numberWithInt:3],
   @"DemoName": @"Timer Theree",
   @"DueDate": [[NSDate date] dateByAddingTimeInterval:180]};

   return [[[NSMutableArray alloc]initWithObjects:demoOne,demoTwo,demoThree,nil]autorelease]; //return a NSMutableArray
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

  // return [mutArray count];
    int k = self.demoList.count;
    if(k > 0)
      return k;
    else
      return 0;
 }

- (void)countdownTimer:(CountdownTimer *)ct didFinishTimerFordemo:(NSNumber *)demoId
{
  // Here I have to delete row having this demoId.
  int count = [self.demoList count];
  int indxTodelete;
  NSString *key = @"DemoId";
  for(int i = 0; i < count ; i++)
  {

     NSDictionary *aDict = [self.demoList objectAtIndex:i];
     NSNumber *aNum = [aDict objectForKey:key];
     if([aNum isEqualToNumber:demoId])
     {
         indxTodelete = i;
     }

 }
  [self.aTableView beginUpdates];
  [self.demoList removeObjectAtIndex:indxTodelete];
   NSIndexPath *indexPath = [NSIndexPath indexPathForRow:indxTodelete inSection:0];
   [self.aTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
   [self.aTableView endUpdates];

 }


希望这可以帮助 :)

于 2013-09-25T07:31:09.157 回答