iPhoneマークシート

センター試験の季節なので、マークシートっぽいiPhoneアプリのサンプルコードを描いてみます。


動かすとこんな感じです

サンプルコード

#import “ViewController.h”

@class MarkCell;

@protocol MarkCellDelegate <NSObject>

– (void)markCell:(MarkCell*)cell answer:(NSString*)answer;

@end

@interface MarkCell : UITableViewCell

@property (nonatomic, weak) UILabel *no;

@property id<MarkCellDelegate> delegate;

@end

#define MarkColor [[UIColor blackColor] colorWithAlphaComponent:0.4]

@implementation MarkCell

– (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {

        float w = CGRectGetMaxX(self.frame) / 12.0;

        float h = 44.0;

        

        UILabel *no = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, w*2.0, h)];

        [self.contentView addSubview:no];

        self.no = no;

        self.no.textColor = [UIColor redColor];

        self.no.textAlignment = NSTextAlignmentCenter;

        

        for (int i=0; i<10; i++) {

            float x = w * (i + 2);

            UILabel *number = [[UILabel alloc] initWithFrame:CGRectMake(x, h * 0.1, w*0.7, h * 0.8)];

            number.tag = i + 1;

            number.text = [@(i + 1) stringValue];

            number.font = [UIFont boldSystemFontOfSize:12];

            number.textColor = [UIColor redColor];

            number.textAlignment = NSTextAlignmentCenter;

            number.layer.borderColor = [UIColor redColor].CGColor;

            number.layer.borderWidth = 1;

            number.layer.cornerRadius = w * 0.3;

            number.layer.masksToBounds = YES;

            [self.contentView addSubview:number];

            

            number.userInteractionEnabled = YES;

            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectAnser:)];

            [number addGestureRecognizer:tap];

        }

        

        self.selectionStyle = UITableViewCellSelectionStyleNone;

    }

    return self;

}

– (void)selectAnser:(UITapGestureRecognizer*)gr

{

    NSPredicate *pred = [NSPredicate predicateWithFormat:@”backgroundColor == %@”, MarkColor];

    NSArray *markedBuf = [self.contentView.subviews filteredArrayUsingPredicate:pred];

   

    

    NSString *ans = [(UILabel*)gr.view text];

    if ([markedBuf count]) {

        // same number –> clear mark

        UILabel *marked = markedBuf[0];

        if ([ans isEqual:marked.text]) ans = @””;

    }

    

    [self updateMark:ans];

    [self.delegate markCell:self answer:ans];

}

– (void)updateMark:(NSString*)answer

{

    NSPredicate *pred = [NSPredicate predicateWithFormat:@”tag > %d”, 0];

    NSArray *answers = [self.contentView.subviews filteredArrayUsingPredicate:pred];

    for (UILabel *ans in answers) {

        ans.backgroundColor = ([ans.text isEqual:answer]) ? MarkColor : [UIColor clearColor];

    }

}

@end

@interface ViewController () <UITableViewDelegate, UITableViewDataSource, MarkCellDelegate>

@property (nonatomic, strong) NSMutableArray *answers;

@end

@implementation ViewController

static NSString *CellIdentifier = @”answerCell”;

– (void)viewDidLoad

{

    [super viewDidLoad];

    UITableView *sheet = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];

    sheet.delegate = self;

    sheet.dataSource = self;

    [self.view addSubview:sheet];

    [sheet registerClass:[MarkCell class] forCellReuseIdentifier:CellIdentifier];

    

    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetMaxX(self.view.frame), 40)];

    header.backgroundColor = [UIColor redColor];

    sheet.tableHeaderView = header;

}

– (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return 50;

}

– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    MarkCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (!cell.delegate)

        cell.delegate = self;

    

    cell.no.text = [@(indexPath.row + 1) stringValue];

    [cell updateMark:self.answers[indexPath.row]];

    

    return cell;

}

– (void)markCell:(MarkCell *)cell answer:(NSString *)answer

{

    [self.answers replaceObjectAtIndex:[cell.no.text intValue] – 1 withObject:answer];

}

– (NSMutableArray *)answers

{

    if (!_answers) {

        _answers = [NSMutableArray array];

        for (int i=0; i<50; i++) [_answers addObject:@””];

        

    }

    return _answers;

}

@end