-
Notifications
You must be signed in to change notification settings - Fork 2
/
QuestionDetailDataSource.m
86 lines (74 loc) · 2.88 KB
/
QuestionDetailDataSource.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//
// QuestionDetailDataSource.m
// BrowseOverflow
//
// Created by Graham Lee on 30/08/2011.
// Copyright (c) 2011 Fuzzy Aliens Ltd. All rights reserved.
//
#import "QuestionDetailDataSource.h"
#import "QuestionDetailCell.h"
#import "AnswerCell.h"
#import "Answer.h"
#import "Question.h"
#import "Person.h"
#import "AvatarStore.h"
enum {
questionSection = 0,
answerSection = 1,
sectionCount
};
@interface QuestionDetailDataSource ()
- (NSString *)HTMLStringForSnippet: (NSString *)snippet;
@end
@implementation QuestionDetailDataSource
@synthesize question;
@synthesize detailCell;
@synthesize answerCell;
@synthesize avatarStore;
- (NSString *)HTMLStringForSnippet:(NSString *)snippet {
return [NSString stringWithFormat: @"<html><head></head><body>%@</body></html>", snippet];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return (section == answerSection) ? [question.answers count] : 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
if (indexPath.section == questionSection) {
[[NSBundle bundleForClass: [self class]] loadNibNamed: @"QuestionDetailCell" owner: self options: nil];
[detailCell.bodyWebView loadHTMLString: [self HTMLStringForSnippet: question.body] baseURL: nil];
detailCell.titleLabel.text = question.title;
detailCell.scoreLabel.text = [NSString stringWithFormat: @"%i", question.score];
detailCell.nameLabel.text = question.asker.name;
detailCell.avatarView.image = [UIImage imageWithData: [avatarStore dataForURL: question.asker.avatarURL]];
cell = detailCell;
self.detailCell = nil;
}
else if (indexPath.section == answerSection) {
Answer *thisAnswer = [question.answers objectAtIndex: indexPath.row];
[[NSBundle bundleForClass: [self class]] loadNibNamed: @"AnswerCell" owner: self options: nil];
answerCell.scoreLabel.text = [NSString stringWithFormat: @"%d", thisAnswer.score];
answerCell.acceptedIndicator.hidden = !thisAnswer.accepted;
Person *answerer = thisAnswer.person;
answerCell.personName.text = answerer.name;
answerCell.personAvatar.image = [UIImage imageWithData: [avatarStore dataForURL: answerer.avatarURL]];
[answerCell.bodyWebView loadHTMLString: [self HTMLStringForSnippet: thisAnswer.text] baseURL: nil];
cell = answerCell;
self.answerCell = nil;
}
else {
NSParameterAssert(indexPath.section < sectionCount);
}
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return sectionCount;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == questionSection) {
return 276.0f;
}
else {
return 201.0f;
}
}
@end