UITableViewDelegate, UITableViewDataSource-CS193P
UITableViewDelegate, UITableViewDataSource
前回plistの読込み、UITableViewCellAccessory-CS193Pの続きになります。
AssignmentPresence2の後半にあたります。
iPhone Application Programming
今迄、作成したソースはhttp://public.me.com/seijit/iPhone/CS193Pから
今回の目標
PersonDetailViewControllerを修正し、下のアプリケーションを完成させるのが目的です。List,Detail共にTableViewを使用しています。ListがUITableViewStylePlain, Detailの方がUITableViewStyleGroupedとStyleを変えて表示をしています。
PersonDetail.xibの変更
PersonDetail.xibをWクリックし、InterfaceBuilderを起動します。ここではViewを大幅に変更します。
View上にUITableViewのみを配置し、AttributesInspectorでTableViewのStyleを"Grouped"にします。
ConnectionsInspectorでPersonDetailViewControllerのviewをTableViewに、TableViewのdataSource,delegateをPersonDetailViewControllerに接続します。
PersonList.xibでは同じTableViewを使っているのにdataSource,delegateの接続は行いませんでした。この違いはPersonLisrViewControllerはTableViewControllerのSubClassに対して、PersonDetailViewControllerはUIViewControllerのSubClassだからです。
PersonDetailViewControllerの修正
PersonDetailViewControllerはPersonListViewControllerとは違った形でTableViewを表示させます。ProtocolであるUITableViewDelegate, UITableViewDataSourceの実装を行います。
PersonDetailViewController
PersonDetailViewController.h
#import <UIKit/UIKit.h>
@class Person;
@interface PersonDetailViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
Person* person;
}
@property (nonatomic, assign) Person* person;
@end
ProtocolであるUITableViewDelegate, UITableViewDataSourceの宣言をしています。
PersonDetailViewController.m
#import "PersonDetailViewController.h"
#import "Person.h"
#import "PersonText.h"
@implementation PersonDetailViewController
@synthesize person;
// 略
@end
UITableViewDelegate, UITableViewDataSourceのメソッドを実装します。
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
return [person.textArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"MyIdentifer";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
PersonText *personText = [person.textArray objectAtIndex:indexPath.row];
UILabel *textField = [[UILabel alloc] initWithFrame:CGRectMake(20, 6, personText.size.width, personText.size.height)];
[textField setText:[NSString stringWithCString:[personText.textString UTF8String] encoding:NSUTF8StringEncoding]];
[textField setBackgroundColor:[UIColor clearColor]];
[textField setLineBreakMode:UILineBreakModeWordWrap];
[textField setFont:personText.font];
[textField setHighlightedTextColor:[UIColor whiteColor]];
[textField setNumberOfLines:0];
[cell addSubview:textField];
[textField release];
return cell;
}
行の数とUITableViewCellの中身の表示を行うメソッドです。UILabelを作成し、UITableViewCellのSubViewとして追加しています。
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
// Return the displayed title for the specified section.
return @"Statuses";
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
PersonText *personText = [person.textArray objectAtIndex:indexPath.row];
int padding = 10;
return personText.size.height + padding;
}
Sectionのヘッダ名を返すメソッドと、各UITableViewCellの高さを返すメソッドです。ここで動的にUITabelViewCellの高さを変化させています。
PersonListViewController
最後にPersonListのUITableViewCellをクリックした際のメソッドを変更しておきます。ここでPersonDetailViewControllerを作成し、UINavigationContollerのStackに追加しています。
PersonListViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here. Create and push another view controller.
PersonDetailViewController *detailViewController = [[PersonDetailViewController alloc] initWithNibName:@"PersonDetail" bundle:nil];
int personIndex = [indexPath indexAtPosition: [indexPath length] - 1];
detailViewController.person = [myData objectAtIndex:personIndex];
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
これでAssignmentPresence2ができました。
今回作成したソースはhttp://public.me.com/seijit/iPhone/CS193P/Lecture9のPresence4.tar.gzです。
TrackBack URL :

No comments yet.
Leave a comment
This is the comment form.