ios - 将可变数组推送到 TableView

标签 ios nsmutablearray

我正在尝试将可变数组推送到 UITableView。但是当我按下按钮时,我的 UITableView 没有得到更新。 tableview 仍然是空的......我已经创建了 tableitems 及其计数的 NSLog。输出中显示的值是正确的。不确定错误是什么。

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>{
    UILabel *lbl;
    NSTimer *stopTimer;
    NSDate *startDate;
    BOOL running,lap;
    UIButton *bttn;
    NSMutableArray *tableItems;
    NSString *timeString;
}

@property (strong,nonatomic) IBOutlet UILabel *lbl;
@property (strong,nonatomic) IBOutlet UIButton *bttn;
@property (strong,nonatomic) NSMutableArray *tableItems;
@property (strong,nonatomic) NSString *timeString;

-(IBAction)startPressed:(id)sender;
-(IBAction)resetPressed:(id)sender;

-(void)updateTimer;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize lbl,bttn,tableItems,timeString;

- (void)viewDidLoad
{
    [super viewDidLoad];
    lbl.text = @"00.00.00.000";
    running = FALSE;
    lap = FALSE;
    startDate = [NSDate date];
    tableItems = [[NSMutableArray alloc] init];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(IBAction)startPressed:(id)sender{
    if(!running){
        running = TRUE;
        lap = TRUE;
        [sender setTitle:@"Stop" forState:UIControlStateNormal];
        [bttn setTitle:@"Lap" forState:UIControlStateNormal];
        if (stopTimer == nil) {
            stopTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
                                                         target:self
                                                       selector:@selector(updateTimer)
                                                       userInfo:nil
                                                        repeats:YES];
        }
    }else{
        running = FALSE;
        lap = FALSE;
        [sender setTitle:@"Start" forState:UIControlStateNormal];
        [bttn setTitle:@"Restart" forState:UIControlStateNormal];
        [stopTimer invalidate];
        stopTimer = nil;
    }

}
-(void)updateTimer{
    NSDate *currentDate = [NSDate date];
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm:ss.SSS"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
    timeString=[dateFormatter stringFromDate:timerDate];
    lbl.text = timeString;
}

-(IBAction)resetPressed:(id)sender{
    if (!lap) {
        [stopTimer invalidate];
        stopTimer = nil;
        startDate = [NSDate date];
        lbl.text = @"00.00.00.000";
        running = FALSE;
    }
    else{
        [tableItems addObject:timeString];
        NSLog(@"%@",tableItems);
        NSLog(@"%i",tableItems.count);
    }

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return tableItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    //Step 1:Check whether if we can reuse a cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    //Step2: If there are no new cells to reuse,create a new one
    if(cell ==  nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
    }

    //Step 3: Set the cell text content
    cell.textLabel.text = [tableItems objectAtIndex:indexPath.row];

    //Step 4: Return the row
    return cell;

}
@end

已将数据源和委托(delegate)链接到界面构建器中的 TableView 。不确定我的错误是什么......

最佳答案

将对象添加到 tableItems 数组不会加载表格。

你需要调用[tableView reloadData];

此外,为此您需要在接口(interface)文件中创建表的导出。

关于ios - 将可变数组推送到 TableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13170719/

相关文章:

ios - 如何将 CGRect 帧值传递给自定义过渡动画 Controller

ios - ManagedObjectContext 保存在内存中,但不在磁盘上

ios - 如何将 NSData 转换为 C 字符串?

ios - 从数组返回对象属性的总值

objective-c - 添加一个字符串对象到 NSMutuableArray

ios - 在 Swift 中使用 UIBezierPath 添加两条路径

iphone - NSMutableArray UIButtons Action ?

ios - 在添加新值之前如何检查数组是否具有相同的对象值

objective-c - 将对象添加到 NSMutableArray 属性

ios - 为什么必须将 View 属性分配给变量?