Analytics

2012年10月19日 星期五

[iOS]UITableView 使用custom(自定) cell


xcode版本 4.5.1

要產生自定的cell可使用1.程式產生 2.用story board去拉

注意這個error 

如果New File...>Objective-C class
選擇加入的檔案若是Subclass of UIViewController "With XIB for user interface"
就要注意他預設的Class是設定為File's owner
不是你後來加入的TableViewCell
這樣才是對的!記得取消File's owner的class名稱設定!
part1.
這邊先記錄用interface builder去拉的方法

[
參考文件
http://www.icodeblog.com/2009/05/24/custom-uitableviewcell-using-interface-builder/
]

2012/11/1簡述版:
1.開一個single view controller 專案
2.在story board裡面 放入UITableView , 按
3.加入objC obj 命名為 CustomCell
4.加入一個xib view
5....待續

原本記錄
1. In your UITableView project
2. right click your project
3. New File...
4. Cocoa Touch > Objective-C class
5. Class name : CustomCell
    Subclass of : UITableViewCell
6. iOS > User Interface > View
7. delete view controller
8. add a "Table View Cell" from xcode objects list on your lower right corner
9. remember to go to the " identity inspector" and setting the class name to fit your CustomCell class name


part2.
從自定的UITableViewCell 裡面的按鈕元件
觸發動作(如NavigationController push)的方法


reference UITableView's navigator controller from a UITableViewCustom Cell?

Road1:from custom cell

way1 : give cell a pointer to navigation controller

in the customcell.h

UINavigationController *naviPointer;
-(IBAction)cellPushNavigation:(id)sender; -> remember connect to UIButton in .xib
@property (nonatomic, retain) UINavigationController *naviPointer

in the customcell.m

@synthesize naviPointer;
-(IBAction)cellPushNavigation:(id)sender
{
    HelloViewController *controller = [[HelloViewControlleralloc] initWithNibName:NSStringFromClass([HelloViewControllerclass]) bundle:nil];
         
     [naviPointer pushViewController:controller animated:YES];
    
}

in the root view controller.m

in the cell making place, add:
 cell.naviPointer = self.navigationController;



way2 :

new project "CustomCellActFromTableView.xcodeproj"

customcell.h

- (IBAction)buttonPressed:(id)sender;

customcell.m

- (IBAction)buttonPressed:(id)sender
{
    
    UITableView *tv = (UITableView *) self.superview;

    UITableViewController *vc = (UITableViewController *) tv.delegate;
    
     HelloViewController *controller = [[HelloViewControlleralloc] initWithNibName:NSStringFromClass([HelloViewControllerclass]) bundle:nil];
   
   [vc.navigationControllerpushViewController:controller animated:YES];
    
}

way2 reference 



Road2: button addTarget

step1. make IBOutlet from CustomCell.xib -> CustomCell.h, don't forget @property & @synthesize
step2. on rootTableView controller 
[cell.button addTarget:self action:@selector(method:) forControlEvents:UIControlEventTouchDown];
     and
-(void)method:(id)sender


update 2012/11/5

add different cell on different row

沒有留言:

張貼留言