Skip to content

Commit

Permalink
Add new SurveyViewCell with AnswerCollectionView and custom AnswerCell
Browse files Browse the repository at this point in the history
  • Loading branch information
Casing Chu committed Feb 21, 2015
1 parent 78b90a4 commit c02c201
Show file tree
Hide file tree
Showing 13 changed files with 467 additions and 39 deletions.
26 changes: 26 additions & 0 deletions unnamed.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
3E312DFD1A96F36700DC747B /* SurveyHeaderCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 3E312DFB1A96F36700DC747B /* SurveyHeaderCell.m */; };
3E312DFE1A96F36700DC747B /* SurveyHeaderCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 3E312DFC1A96F36700DC747B /* SurveyHeaderCell.xib */; };
3E312E011A970EBC00DC747B /* BarView.m in Sources */ = {isa = PBXBuildFile; fileRef = 3E312E001A970EBC00DC747B /* BarView.m */; };
3E58B4CB1A98229200AA14D4 /* SurveyViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 3E58B4C91A98229200AA14D4 /* SurveyViewCell.m */; };
3E58B4CC1A98229200AA14D4 /* SurveyViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 3E58B4CA1A98229200AA14D4 /* SurveyViewCell.xib */; };
3E58B4CF1A98300C00AA14D4 /* AnswerCollectionView.m in Sources */ = {isa = PBXBuildFile; fileRef = 3E58B4CE1A98300C00AA14D4 /* AnswerCollectionView.m */; };
3E58B4D41A9832BF00AA14D4 /* AnswerView.m in Sources */ = {isa = PBXBuildFile; fileRef = 3E58B4D31A9832BF00AA14D4 /* AnswerView.m */; };
3E58B4D61A984C3900AA14D4 /* AnswerView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 3E58B4D51A984C3900AA14D4 /* AnswerView.xib */; };
3E7C1D4F1A958BC10046FDE1 /* HomeViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 3E7C1D4D1A958BC10046FDE1 /* HomeViewController.m */; };
3E7C1D501A958BC10046FDE1 /* HomeViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 3E7C1D4E1A958BC10046FDE1 /* HomeViewController.xib */; };
3E7C1D541A958D120046FDE1 /* SurveyCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 3E7C1D521A958D120046FDE1 /* SurveyCell.m */; };
Expand Down Expand Up @@ -74,6 +79,14 @@
3E312DFC1A96F36700DC747B /* SurveyHeaderCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = SurveyHeaderCell.xib; sourceTree = "<group>"; };
3E312DFF1A970EBC00DC747B /* BarView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BarView.h; sourceTree = "<group>"; };
3E312E001A970EBC00DC747B /* BarView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BarView.m; sourceTree = "<group>"; };
3E58B4C81A98229200AA14D4 /* SurveyViewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SurveyViewCell.h; sourceTree = "<group>"; };
3E58B4C91A98229200AA14D4 /* SurveyViewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SurveyViewCell.m; sourceTree = "<group>"; };
3E58B4CA1A98229200AA14D4 /* SurveyViewCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = SurveyViewCell.xib; sourceTree = "<group>"; };
3E58B4CD1A98300C00AA14D4 /* AnswerCollectionView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AnswerCollectionView.h; sourceTree = "<group>"; };
3E58B4CE1A98300C00AA14D4 /* AnswerCollectionView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AnswerCollectionView.m; sourceTree = "<group>"; };
3E58B4D21A9832BF00AA14D4 /* AnswerView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AnswerView.h; sourceTree = "<group>"; };
3E58B4D31A9832BF00AA14D4 /* AnswerView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AnswerView.m; sourceTree = "<group>"; };
3E58B4D51A984C3900AA14D4 /* AnswerView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = AnswerView.xib; sourceTree = "<group>"; };
3E7C1D4C1A958BC10046FDE1 /* HomeViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HomeViewController.h; sourceTree = "<group>"; };
3E7C1D4D1A958BC10046FDE1 /* HomeViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HomeViewController.m; sourceTree = "<group>"; };
3E7C1D4E1A958BC10046FDE1 /* HomeViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = HomeViewController.xib; sourceTree = "<group>"; };
Expand Down Expand Up @@ -296,6 +309,14 @@
B862A88B1A970E25008B2310 /* DetailAnswerCell.xib */,
B827A9E61A9815E100AB43CE /* ProfileImageView.h */,
B827A9E71A9815E100AB43CE /* ProfileImageView.m */,
3E58B4C81A98229200AA14D4 /* SurveyViewCell.h */,
3E58B4C91A98229200AA14D4 /* SurveyViewCell.m */,
3E58B4CA1A98229200AA14D4 /* SurveyViewCell.xib */,
3E58B4CD1A98300C00AA14D4 /* AnswerCollectionView.h */,
3E58B4CE1A98300C00AA14D4 /* AnswerCollectionView.m */,
3E58B4D21A9832BF00AA14D4 /* AnswerView.h */,
3E58B4D31A9832BF00AA14D4 /* AnswerView.m */,
3E58B4D51A984C3900AA14D4 /* AnswerView.xib */,
);
name = View;
sourceTree = "<group>";
Expand Down Expand Up @@ -430,13 +451,15 @@
E25DBF071A9489530074D48B /* TabBarMenuViewController.xib in Resources */,
E2C6FF1F1A96C178005AEDA9 /* ProfileCell.xib in Resources */,
B8C291C31A930BDD000F2143 /* LaunchScreen.xib in Resources */,
3E58B4D61A984C3900AA14D4 /* AnswerView.xib in Resources */,
3E7C1D551A958D120046FDE1 /* SurveyCell.xib in Resources */,
3E7C1D5A1A95AA520046FDE1 /* SurveyViewController.xib in Resources */,
B862A88D1A970E25008B2310 /* DetailAnswerCell.xib in Resources */,
B8C291C01A930BDD000F2143 /* Images.xcassets in Resources */,
3E7C1D691A966CBA0046FDE1 /* AnswerCell.xib in Resources */,
B87C15BD1A93113400191823 /* LoginViewController.xib in Resources */,
B8C0DBDD1A95138C0023BC39 /* ComposeAnswerCell.xib in Resources */,
3E58B4CC1A98229200AA14D4 /* SurveyViewCell.xib in Resources */,
B8C0DBCE1A94F71A0023BC39 /* ComposeViewController.xib in Resources */,
3E7C1D501A958BC10046FDE1 /* HomeViewController.xib in Resources */,
3E312DFE1A96F36700DC747B /* SurveyHeaderCell.xib in Resources */,
Expand Down Expand Up @@ -494,6 +517,8 @@
B8BA93E81A9459B5003BD476 /* User.m in Sources */,
B827A9F41A982B2600AB43CE /* UIColor+AppBgColor.m in Sources */,
B8C0DBF11A95C1DA0023BC39 /* Survey.m in Sources */,
3E58B4D41A9832BF00AA14D4 /* AnswerView.m in Sources */,
3E58B4CB1A98229200AA14D4 /* SurveyViewCell.m in Sources */,
B8C704AA1A97065600243C3B /* DetailQuestionCell.m in Sources */,
3E2BB7E01A94FF0000E78B3A /* AppNavigationController.m in Sources */,
3E312E011A970EBC00DC747B /* BarView.m in Sources */,
Expand All @@ -509,6 +534,7 @@
3E7C1D4F1A958BC10046FDE1 /* HomeViewController.m in Sources */,
3E7C1D591A95AA520046FDE1 /* SurveyViewController.m in Sources */,
B8BA93EB1A9464C7003BD476 /* Question.m in Sources */,
3E58B4CF1A98300C00AA14D4 /* AnswerCollectionView.m in Sources */,
E25DBF061A9489530074D48B /* TabBarMenuViewController.m in Sources */,
B827A9EE1A98287B00AB43CE /* GrayBarButtonItem.m in Sources */,
B8C0DBDC1A95138C0023BC39 /* ComposeAnswerCell.m in Sources */,
Expand Down
4 changes: 2 additions & 2 deletions unnamed/AnswerCell.xib
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="100.0%" textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Z5O-T8-rF8">
<rect key="frame" x="51" y="2" width="36.5" height="14.5"/>
<rect key="frame" x="51" y="2" width="37" height="15"/>
<constraints>
<constraint firstAttribute="width" constant="36.5" id="2gR-L4-GNm"/>
<constraint firstAttribute="width" constant="28" id="dKF-Hk-6ab"/>
Expand All @@ -29,7 +29,7 @@
</variation>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="1. The Answer Is" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="R64-le-eDB">
<rect key="frame" x="95.5" y="3" width="78" height="12"/>
<rect key="frame" x="96" y="3" width="78" height="12"/>
<fontDescription key="fontDescription" type="system" pointSize="10"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
Expand Down
15 changes: 15 additions & 0 deletions unnamed/AnswerCollectionView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// AnswerCollectionView.h
// unnamed
//
// Created by Casing Chu on 2/20/15.
// Copyright (c) 2015 com.yahoo. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AnswerCollectionView : UIView

@property (nonatomic, strong) NSArray *answers;

@end
92 changes: 92 additions & 0 deletions unnamed/AnswerCollectionView.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//
// AnswerCollectionView.m
// unnamed
//
// Created by Casing Chu on 2/20/15.
// Copyright (c) 2015 com.yahoo. All rights reserved.
//

#import "AnswerCollectionView.h"
#import "AnswerView.h"
#import "Answer.h"

@interface AnswerCollectionView ()

@property (nonatomic, assign) NSInteger total;
@property (nonatomic, strong) NSMutableArray *answerViews;

- (NSInteger)getTotalFromAnswers:(NSArray *)answers;

@end

@implementation AnswerCollectionView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/

- (void)baseInit {
self.answerViews = [[NSMutableArray alloc] init];
self.total = 0;
}

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self baseInit];
}
return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
[self baseInit];
}
return self;
}

- (void)setAnswers:(NSArray *)answers {
_answers = answers;
_total = [self getTotalFromAnswers:answers];

int count = 1;
[self.answerViews removeAllObjects];
for (Answer *answer in self.answers) {
AnswerView *view = [[AnswerView alloc] init];
view.index = count;
view.total = self.total;
view.answer = answer;
count++;
[self.answerViews addObject:view];
[self addSubview:view];
}

[self setNeedsLayout];
}

- (void)layoutSubviews {
[super layoutSubviews];

for (int i=0;i<self.answerViews.count;i++) {
AnswerView *view = self.answerViews[i];
CGRect imageFrame = CGRectMake(0, view.frame.size.height * i, self.frame.size.width, self.frame.size.height);
view.frame = imageFrame;
}

}

#pragma mark - Private Methods
- (NSInteger)getTotalFromAnswers:(NSArray *)answers {
NSInteger total = 0;
for (Answer *ans in answers) {
total += ans.count;
}
return total;
}

@end
18 changes: 18 additions & 0 deletions unnamed/AnswerView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// AnswerView.h
// unnamed
//
// Created by Casing Chu on 2/20/15.
// Copyright (c) 2015 com.yahoo. All rights reserved.
//

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

@interface AnswerView : UIView

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

@end
65 changes: 65 additions & 0 deletions unnamed/AnswerView.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// AnswerView.m
// unnamed
//
// Created by Casing Chu on 2/20/15.
// Copyright (c) 2015 com.yahoo. All rights reserved.
//

#import "AnswerView.h"
#import "BarView.h"

@interface AnswerView ()
@property (weak, nonatomic) IBOutlet BarView *barView;
@property (weak, nonatomic) IBOutlet UILabel *percentLabel;
@property (weak, nonatomic) IBOutlet UILabel *answerLabel;
@property (strong, nonatomic) IBOutlet UIView *containerView;

@end

@implementation AnswerView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/

- (void)baseInit {
UINib *nib = [UINib nibWithNibName:@"AnswerView" bundle:nil];
[nib instantiateWithOwner:self options:nil];

self.containerView.frame = self.bounds;
self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self addSubview:self.containerView];
}

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self baseInit];
}
return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
[self baseInit];
}
return self;
}

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

[self setNeedsLayout];
}

@end
71 changes: 71 additions & 0 deletions unnamed/AnswerView.xib
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="6254" systemVersion="14C109" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6247"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="AnswerView">
<connections>
<outlet property="answerLabel" destination="6zp-Vs-ovw" id="uNi-7F-f3E"/>
<outlet property="barView" destination="O7j-Bf-ACJ" id="sjf-gt-xEr"/>
<outlet property="containerView" destination="m8k-6m-S8c" id="yYm-jl-tr7"/>
<outlet property="percentLabel" destination="R6c-5O-Ip8" id="hBo-lB-O6C"/>
</connections>
</placeholder>
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
<view contentMode="scaleToFill" id="m8k-6m-S8c">
<rect key="frame" x="0.0" y="0.0" width="320" height="24"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="100.0%" textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="R6c-5O-Ip8">
<rect key="frame" x="51" y="2" width="43.5" height="14.5"/>
<constraints>
<constraint firstAttribute="width" constant="28" id="3rh-hP-pRR"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="12"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
<variation key="default">
<mask key="constraints">
<exclude reference="3rh-hP-pRR"/>
</mask>
</variation>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="1. The Answer Is" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="6zp-Vs-ovw">
<rect key="frame" x="98" y="2" width="93" height="14.5"/>
<fontDescription key="fontDescription" type="system" pointSize="12"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="O7j-Bf-ACJ" customClass="BarView">
<rect key="frame" x="8" y="5" width="40" height="8"/>
<color key="backgroundColor" white="0.66666666666666663" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstAttribute="width" constant="40" id="0l3-oJ-CVJ"/>
<constraint firstAttribute="height" constant="8" id="eTG-9f-CWP"/>
</constraints>
</view>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="R6c-5O-Ip8" firstAttribute="centerY" secondItem="6zp-Vs-ovw" secondAttribute="centerY" id="AzX-HI-mZd"/>
<constraint firstItem="R6c-5O-Ip8" firstAttribute="centerY" secondItem="O7j-Bf-ACJ" secondAttribute="centerY" id="FzZ-ed-C5P"/>
<constraint firstItem="6zp-Vs-ovw" firstAttribute="leading" secondItem="R6c-5O-Ip8" secondAttribute="trailing" constant="3" id="NXq-7s-fxT"/>
<constraint firstAttribute="trailing" relation="greaterThanOrEqual" secondItem="6zp-Vs-ovw" secondAttribute="trailing" constant="8" id="aqr-RD-inv"/>
<constraint firstItem="O7j-Bf-ACJ" firstAttribute="centerY" secondItem="6zp-Vs-ovw" secondAttribute="centerY" id="b9l-Fr-vO8"/>
<constraint firstItem="O7j-Bf-ACJ" firstAttribute="top" secondItem="m8k-6m-S8c" secondAttribute="top" constant="5" id="dpT-8x-DnB"/>
<constraint firstItem="O7j-Bf-ACJ" firstAttribute="centerY" secondItem="R6c-5O-Ip8" secondAttribute="centerY" id="f0l-31-lgq"/>
<constraint firstItem="R6c-5O-Ip8" firstAttribute="leading" secondItem="O7j-Bf-ACJ" secondAttribute="trailing" constant="3" id="fJp-K3-wkR"/>
<constraint firstItem="O7j-Bf-ACJ" firstAttribute="leading" secondItem="m8k-6m-S8c" secondAttribute="leading" constant="8" id="ikq-q4-iQg"/>
<constraint firstItem="R6c-5O-Ip8" firstAttribute="leading" secondItem="O7j-Bf-ACJ" secondAttribute="trailing" constant="3" id="onP-rZ-joU"/>
</constraints>
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
<point key="canvasLocation" x="145" y="-203"/>
</view>
</objects>
<simulatedMetricsContainer key="defaultSimulatedMetrics">
<simulatedStatusBarMetrics key="statusBar"/>
<simulatedOrientationMetrics key="orientation"/>
<simulatedScreenMetrics key="destination" type="retina4"/>
</simulatedMetricsContainer>
</document>
22 changes: 22 additions & 0 deletions unnamed/BarView.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,30 @@

@implementation BarView

- (void)baseInit {
self.percentage = 0.0;
}

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self baseInit];
}
return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
[self baseInit];
}
return self;
}

- (void)setPercentage:(CGFloat)percentage {
_percentage = percentage;

[self setNeedsLayout];
}

/*
Expand Down
Loading

0 comments on commit c02c201

Please sign in to comment.