首页 > 代码库 > iOS.UIKit.13.UITableView -- Simple

iOS.UIKit.13.UITableView -- Simple

 

1、案例介绍:最简单的表视图,如图01

图01

2、team.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <array>
        <dict>
            <key>name</key>
            <string>A1-南非</string>
            <key>image</key>
            <string>SouthAfrica</string>
        </dict>
        <dict>
            <key>name</key>
            <string>A2-墨西哥</string>
            <key>image</key>
            <string>Mexico</string>
        </dict>
        <dict>
            <key>name</key>
            <string>B1-阿根廷</string>
            <key>image</key>
            <string>Argentina</string>
        </dict>
        <dict>
            <key>name</key>
            <string>B2-尼日利亚</string>
            <key>image</key>
            <string>Nigeria</string>
        </dict>
        <dict>
            <key>name</key>
            <string>C1-英格兰</string>
            <key>image</key>
            <string>England</string>
        </dict>
        <dict>
            <key>name</key>
            <string>C2-美国</string>
            <key>image</key>
            <string>USA</string>
        </dict>
        <dict>
            <key>name</key>
            <string>D1-德国</string>
            <key>image</key>
            <string>Germany</string>
        </dict>
        <dict>
            <key>name</key>
            <string>D2-澳大利亚</string>
            <key>image</key>
            <string>Australia</string>
        </dict>
        <dict>
            <key>name</key>
            <string>E1-荷兰</string>
            <key>image</key>
            <string>Holland</string>
        </dict>
        <dict>
            <key>name</key>
            <string>E2-丹麦</string>
            <key>image</key>
            <string>Denmark</string>
        </dict>
        <dict>
            <key>name</key>
            <string>G1-巴西</string>
            <key>image</key>
            <string>Brazil</string>
        </dict>
        <dict>
            <key>name</key>
            <string>G2-朝鲜</string>
            <key>image</key>
            <string>NorthKorea</string>
        </dict>
        <dict>
            <key>name</key>
            <string>H1-西班牙</string>
            <key>image</key>
            <string>Spain</string>
        </dict>
        <dict>
            <key>name</key>
            <string>H2-瑞士</string>
            <key>image</key>
            <string>Switzerland</string>
        </dict>
    </array>
</plist>

3、图片

Algeria.png、Argentina.png、Australia.png、Brazil.png、Cameroon.png、

Chile.png、CoteDIvoire.png、Denmark.png、England.png、France.png、

Germany.png、Ghana.png、Greece.png、Holland.png、Honduras.png、

Italy.png、Japan.png、Mexico.png、NewZealand.png、Nigeria.png、

NorthKorea.png、Paraguay.png、Portugal.png、Serbia.png、Slovakia.png、

Slovenia.png、SouthAfrica.png、SouthKorea.png、Spain.png、Switzerland.png、

Uruguay.png、USA.png

4、Main.storyboard,如图02

图02

5、.h

#import <UIKit/UIKit.h>

@interface CQ21ViewController : UITableViewController

@property (nonatomic, strong) NSArray *listTeams;

@end

6、.m

#import "CQ21ViewController.h"

@interface CQ21ViewController ()

@end

@implementation CQ21ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 加载球队数据
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *plistPath = [bundle pathForResource:@"team" ofType:@"plist"];
    self.listTeams = [[NSArray alloc] initWithContentsOfFile:plistPath];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark UITableViewDataSource协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // 返回表格的行数
    return self.listTeams.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1、加载cell
    static NSString *CellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    // 2、设置cell的textLabel
    NSUInteger row = [indexPath row];
    NSDictionary *rowDict = [self.listTeams objectAtIndex:row];
    cell.textLabel.text = [rowDict objectForKey:@"name"];
    
    // 3、设置cell的图片
    NSString *imagePath = [rowDict objectForKey:@"image"];
    imagePath = [imagePath stringByAppendingString:@".png"];
    cell.imageView.image = [UIImage imageNamed:imagePath];
    
    // 4、设置cell扩展视图为扩展指示器
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

@end