简单表视图

来源:互联网 发布:通信录管理系统c语言 编辑:程序博客网 时间:2024/06/10 04:46

只能说自己能力太差,刚开始学习表视图,错误百出,又是折腾几天,才整明白个简单的表视图。现在总结下基于StoryBoard和自定义的表视图:


一,基于StoryBoard的表视图:

1,新建一个TableTest工程,打开故事板,将原来的ViewController给删掉,重新添加一个 Table View Controller;

2,将ViewController.h文件中的@interface ViewController :UIViewController改成:@interface ViewController : UITableViewController;

3,在Table View controller Scene中,点击table view controller,将标识检查器下的class改为ViewController,如下图(1);

4,将table view属性检查器下的content设置为 DynamicPrototypes(动态表),并将prototype cells 设置为0,如图(2);

                              
                     图(1)                                                                                                     图(2)

5,在Supporting文件底下,添加需显示的图片,且新建一个.plist文件存放之后需在表格中显示的信息,如下图(3),图(4):
                           
                    图(3)                                                                                                         图(4)

备注:我在这个地方就犯了一个打错!!只知道新建一个.plist文件,却不知需要将原图片文件也添加到这个supporting文件下,而是在.plist文件中image后添加图片的路径,以为程序能够根据这个路径找到,如下图(5)所示,这应该算是没有弄清楚.plist文件和supporting文件的作用吧。后果就是程序可以运行,但是表视图中一直不能出现图标,如图(6)所示。

                                                            图(5)



                                  图(6)

6,接下来就应该设计ViewController.h/.m文件中的代码了:
(1),

#import <UIKit/UIKit.h>


@interface ViewController :UITableViewController

@property(nonatomic,strong)NSArray *listTeams;


@end


(2),

#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad

{

    [superviewDidLoad];

    

   NSBundle *bundle=[NSBundlemainBundle];

   NSString *plistPath=[bundlepathForResource:@"team"ofType:@"plist"];

    self.listTeams=[[NSArrayalloc]initWithContentsOfFile:plistPath];

    

// Do any additional setup after loading the view, typically from a nib.

}


- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


//这两个方法是必须实现的


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    

   return [self.listTeamscount];

    

}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    

    //CellIndentifier是重用单元格标识符

   staticNSString *CellIndentifier=@"CellIndentifier";

   UITableViewCell *cell=(UITableViewCell *)[tableViewdequeueReusableCellWithIdentifier:CellIndentifier];



   //我们将prototype属性设置为0了,因此我们需要通过代码来实现单元格的创建


   if(cell==nil){

        

        cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIndentifier];

    }

    

   NSUInteger row=[indexPathrow];

   NSDictionary *rowDict=[self.listTeamsobjectAtIndex:row];

    cell.textLabel.text=[rowDictobjectForKey:@"name"];

    

   NSString *imagePath=[rowDictobjectForKey:@"image"];

    imagePath=[imagePathstringByAppendingString:@".png"];

    cell.imageView.image=[UIImageimageNamed:imagePath];

    

    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;

   return cell;

    

}



@end


7,最终结果如下图(7)(没啥好图,就拿这些瞎凑合了):



                                 图(7)

二,自定义的表视图:
自定义的表视图原理上与在storyboard中创建的表视图相同,只是需要我们


0 0