User Interface->View->命名cellView 2.往上面拖放一個UITableViewCell,然后向其中拖放添加UILabel,UIText..."/>
溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

[IOS]用自定義的cell來創(chuàng)建UITableView

發(fā)布時間:2020-07-16 17:14:07 來源:網絡 閱讀:521 作者:蓬萊仙羽 欄目:移動開發(fā)

步驟:

1.創(chuàng)建自定義的CellView.xib,操作New File->User Interface->View->命名cellView

2.往上面拖放一個UITableViewCell,然后向其中拖放添加UILabel,UITextField,UIButton,如下圖:

[IOS]用自定義的cell來創(chuàng)建UITableView

3.創(chuàng)建一個類Cell,操作New File->Cocoa Touch->Objective-C class->Class:Cell,Subclass of:UITableViewCell

4.將cellView.xib中的Cell的class設置為Cell類,然后可以將label關聯到Cell.h中,也可以設置Label的Tag屬性為1,后面有兩種方法調用xib文件,一種是通過tag,其次就是通過cell。注意:xib文件中的File's Owner只能關聯到viewController,這里自定義的cell.xib沒有可以關聯的contrller文件,所以不需要寫,只需要把xib中的cell控件關聯到cell類就可以了

5.ViewController.h:

#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UITableViewDataSource,UITableViewDataSource> @property (retain, nonatomic) IBOutlet UITableView *tableView; @property(retain,nonatomic)NSArray *arr; @end
6.ViewController.m:

#import "ViewController.h" #import "Cell.h"  static NSString *strIdentifier = @"strIdentifier"; @interface ViewController ()  @end  @implementation ViewController  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];     if (self) {         // Custom initialization     }     return self; }  - (void)viewDidLoad {     [super viewDidLoad];     self.arr = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5", nil];     //獲得包含tableViewcell的nib文件     UINib *nib = [UINib nibWithNibName:@"cellView" bundle:nil];     //用這個nib文件注冊成tableView中表示為strIdentifier的cell     [self.tableView registerNib:nib forCellReuseIdentifier:strIdentifier]; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {     return [self.arr count]; }  -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {     //詢問是否有未使用的,表示為strIdentifier的cell,如果沒有,并且tableview注冊過,系統(tǒng)會建一個新的cell     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strIdentifier];     //用這個自定義的cell可以用綁定的方式修改內容,要將自定義的cell.xib的class修改成Cell類     ((Cell *)cell).label.text = [self.arr objectAtIndex:[indexPath row]];          //也可以用tag的方式查找到需要修改的view     //((UILabel *)[cell viewWithTag:1]).text = [self.arr objectAtIndex:[indexPath row]];     return cell; }  - (void)didReceiveMemoryWarning {     [super didReceiveMemoryWarning];     // Dispose of any resources that can be recreated. }  - (void)dealloc {     [_tableView release];     [self.arr release];     [super dealloc]; } @end 
運行結果圖:

[IOS]用自定義的cell來創(chuàng)建UITableView


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI