<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ### 有時候我們要使UITableView顯示的數據更具可觀性,更美化,就只能在視圖控制器的.m文件中用代碼一句一句地去寫,這樣就會需要繁雜大量的代碼。不可否認,有時候人是很懶惰的,其中的最佳解決辦法就是自定義一個UITableViewCell,控件可直接在上面拖拽,設置尺寸大小顏色等。 ### 一: 1)首先創建一個空的.xib文件 ![](https://box.kancloud.cn/2016-06-12_575cce7da0937.png) 2)創建UITableVeiwCell ![](https://box.kancloud.cn/2016-06-12_575cce7dd1aa5.png) 3)修改繼承類 ![](https://box.kancloud.cn/2016-06-12_575cce7df1410.png) 4)創建輸出口,并添加(拖拽)組件(圖片,lable)等。可見其中組建可隨意認定大小,位置尺寸,還有顏色。 ![](https://box.kancloud.cn/2016-06-12_575cce7e2dc04.png) 5)設置lable的tag,方便以后的賦值。 ![](https://box.kancloud.cn/2016-06-12_575cce7e87f2c.png) 哦,自定義UITableViewCell就算完成了,那么就讓我們看看是怎么把他加到UITableView上的。 ### 二: 1)其實這一步是創建項目時最先完成的。到這里才寫,是為了講解方便。 按正常創建一個表視圖的步驟走,前面的文章已經提到過創表過程。看結果: ![](https://box.kancloud.cn/2016-06-12_575cce7ea7c7a.png) 一個在正常不過的表了,不需要任何修改。 2)關鍵的部分是在控制器的.m文件。使剛才自定義的Cell呈現在UITableVeiw上。 .h文件 ~~~ #import <UIKit/UIKit.h> @interface MyUITableViewYourSelfCellControl : UIViewController <UITableViewDataSource,UITableViewDelegate> { UITableView *MyUITableView; UITableViewCell *MyTableViewCell; UITableViewCell *Mycell02; } @property (nonatomic, strong) IBOutlet UITableView *MyUITableView; @property (nonatomic, strong) IBOutlet UITableViewCell *MyTableViewCell; @property(nonatomic,retain) NSArray *Mypeople; @end ~~~ .m文件 ~~~ #import "MyUITableViewYourSelfCellControl.h" @implementation MyUITableViewYourSelfCellControl @synthesize MyUITableView; @synthesize MyTableViewCell; @synthesize Mypeople; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } #pragma mark - View lifecycle - (void)viewDidLoad { //定義數據 NSDictionary *dic1=[[NSDictionary alloc] initWithObjectsAndKeys:@"dong",@"name",@"22",@"age", nil]; NSDictionary *dic2=[[NSDictionary alloc] initWithObjectsAndKeys:@"wang",@"name",@"23",@"age", nil]; NSDictionary *dic3=[[NSDictionary alloc] initWithObjectsAndKeys:@"zhang",@"name",@"24",@"age", nil]; NSDictionary *dic4=[[NSDictionary alloc] initWithObjectsAndKeys:@"li",@"name",@"25",@"age", nil]; NSDictionary *dic5=[[NSDictionary alloc] initWithObjectsAndKeys:@"sui",@"name",@"26",@"age", nil]; NSArray *people=[NSArray arrayWithObjects:dic1,dic2,dic3,dic4,dic5, nil]; self.Mypeople=people; [super viewDidLoad]; // Do any additional setup after loading the view from its nib. } - (void)viewDidUnload { [self setMyUITableView:nil]; [self setMyTableViewCell:nil]; [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait); } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [self.Mypeople count]; } -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSInteger row=[indexPath row]; static NSString *Tagtittle=@"dong"; UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:Tagtittle]; if (cell==nil) { //最關鍵的就是這句。加載自己定義的nib文件 NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"Empty" owner:self options:nil]; //此時nib里含有的是組件個數 //(感謝網友 電子咖啡 的建議) if ([nib count]>0) { //cell=self.MyTableViewCell for (UIView *v in nib) { [v isKindOfClass:[UITableViewCell class]]?(cell=(UITableViewCell *)v):nil; } } NSDictionary *dic=[self.Mypeople objectAtIndex:row]; NSString *name=[dic objectForKey:@"name"]; NSString *age=[dic objectForKey:@"age"]; //通過設定的tag值來找對應的lable,給其復制。 UILabel *lablename=(UILabel*)[cell viewWithTag:1]; lablename.text=name; UILabel *lableage=(UILabel*)[cell viewWithTag:2]; lableage.text=age; return cell; } @end ~~~ 到這里我們需要了解一下bundle:是一個目錄,包含了圖像,聲音,代碼,nib文件,plist文件等應用程序的資源。 cocoa為我們提供了NSBundle類。其實我們的應用程序就是一個bundle。他也包含了如上面所述的資源。這里我們把它叫做mainbundle。 而此程序里這句?NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"Empty" owner:self options:nil];。加載nib文件。可以看做是先把Empty.xib加載到mainbundle里,等程序需要時再拿出來,賦給了這里的變量nib,然后就相當于把nib文件導進來一樣,可以用其中的組件了。 運行效果: ![](https://box.kancloud.cn/2016-06-12_575cce7ed3c6a.png)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看