Skip to content

Commit

Permalink
Add some tableview style and setup AnswerCell
Browse files Browse the repository at this point in the history
  • Loading branch information
Casing Chu committed Feb 19, 2015
1 parent 043b70e commit 8510414
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
7 changes: 5 additions & 2 deletions unnamed/AnswerCell.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
//

#import <UIKit/UIKit.h>
#import "Answer.h"

@interface AnswerCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *percentLabel;
@property (weak, nonatomic) IBOutlet UILabel *answerLabel;

@property (nonatomic, strong) Answer* answer;
@property (nonatomic, assign) NSInteger total;
@property (nonatomic, assign) NSInteger index;

@end
14 changes: 14 additions & 0 deletions unnamed/AnswerCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@

#import "AnswerCell.h"

@interface AnswerCell ()

@property (weak, nonatomic) IBOutlet UILabel *percentLabel;
@property (weak, nonatomic) IBOutlet UILabel *answerLabel;

@end

@implementation AnswerCell

- (void)awakeFromNib {
Expand All @@ -21,4 +28,11 @@ - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
// Configure the view for the selected state
}

- (void)setAnswer:(Answer *)answer {
_answer = answer;
self.answerLabel.text = [NSString stringWithFormat:@"%ld. %@", self.index, answer.text];
NSInteger percentage = self.total <= 0 ? 0 : 100.0f * answer.count / self.total;
self.percentLabel.text = [NSString stringWithFormat:@"%ld%%", percentage];
}

@end
7 changes: 3 additions & 4 deletions unnamed/HomeViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,9 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
AnswerCell *cell = [self.tableView dequeueReusableCellWithIdentifier:kAnswerCell];
Survey *survey = self.surveys[indexPath.section];
Answer *ans = survey.answers[indexPath.row];
NSInteger total = [self getTotalFromAnswers:survey.answers];
cell.answerLabel.text = [NSString stringWithFormat:@"%ld. %@", indexPath.row, ans.text];
NSInteger percentage = total <= 0 ? 0 : 100.0f * ans.count / total;
cell.percentLabel.text = [NSString stringWithFormat:@"%ld%%", percentage];
cell.index = indexPath.row;
cell.total = [self getTotalFromAnswers:survey.answers];
cell.answer = ans;
return cell;
}

Expand Down
2 changes: 1 addition & 1 deletion unnamed/HomeViewController.xib
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="22" sectionFooterHeight="22" translatesAutoresizingMaskIntoConstraints="NO" id="s02-fg-eh5">
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" style="plain" separatorStyle="none" rowHeight="44" sectionHeaderHeight="22" sectionFooterHeight="22" translatesAutoresizingMaskIntoConstraints="NO" id="s02-fg-eh5">
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</tableView>
Expand Down

0 comments on commit 8510414

Please sign in to comment.