日別アーカイブ: 2014年9月1日

テーブルビューでカスタムセルを使う。

平成26年9月1日(月曜日)〜

XIBファイル作成
file → new → user interface → empty
uitablebiewcellドラッグ
label ドラッグ
button ドラッグ
設定 custom
設定 identifier Cell

CLASSファイル作成
TableViewCustomCell

両者のリンク
@property (weak, nonatomic) IBOutlet UILabel *str1;
@synthesize str1;

segue設定(showDetail)

UIViewController

[objc]
@property (strong, nonatomic) IBOutlet UITableView *tableView;
[/objc]

TableViewContoroller.m

[objc]
#import TableViewCustomCell.h;
[/objc]

nibにて使用

[objc]
static NSString * const TableViewCustomCellIdentifier = TableViewCustomCell;
[/objc]

viewdidload

[objc]
– (void)viewDidLoad
{
[super viewDidLoad];
// カスタマイズしたセルをテーブルビューにセット
UINib *nib = [UINib nibWithNibName:TableViewCustomCellIdentifier bundle:nil];
[self.tableView registerNib:nib forCellReuseIdentifier:@"Cell];
//[self.searchDisplayController.searchResultsTableView registerNib:nib forCellReuseIdentifier:@"Cell];
}
[/objc]

cellForRowAtIndexPath

[objc]
// テーブルセル表示① cellForRowAtIndexPath
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
– (TableViewCustomCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

// 重要 カスタムセル
//TableViewCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier ];
TableViewCustomCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier ];

/*
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
*/

if (cell == nil) {
cell = [[TableViewCustomCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}

[self configureCell:cell atIndexPath:indexPath];
return cell;
}
[/objc]

heightForRowAtIndexPath

[objc]
– (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [TableViewCustomCell rowHeight];
}
[/objc]

didSelectRowAtIndexPath

[objc]
– (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// The table view should not be re-orderable.
//NSLog(@"push");

// セグエで画面遷移させる → prepareForSegueが発生
//prepareForSegueが発生
[self performSegueWithIdentifier:@"showDetail" sender:self.tableView];
}
[/objc]

configureCell

[objc]
– (void)configureCell:(TableViewCustomCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
//cell.textLabel.text = [[object valueForKey:@"timeStamp"] description];
cell.name.text = [[object valueForKey:@"timeStamp"] description];

}
[/objc]

UIBUTTON

平成26年9月1日(月曜日)

XIB設定(button設定)
TYPE →CUSTOM
STATECONFIG→ SELECTED

TableViewCustomCell.h

[objc]
@property (weak, nonatomic) IBOutlet UIButton *button; @end
[/objc]

 

TableViewCustomCell.m

ボタン押下

[objc]
<pre>- (IBAction)press_button:(UIButton *)sender {

// _button.selected = true;
NSLog(@"press!!");
if (_button.selected == true) {
_button.selected = false;
} else if(_button.selected == false) {
_button.selected = true;
}
}
[/objc]

ON OFF 背景画像切り替え

[objc]
– (void)awakeFromNib
{
// Initialization code
NSLog(@"press!!");
[self.button setImage:[UIImage imageNamed:@"5.png"] forState:UIControlStateNormal];
[self.button setImage:[UIImage imageNamed:@"3.png"] forState:UIControlStateSelected];
}
[/objc]