iOS.UIKit.13.UITableView -- Simple

 

一、案例介紹:最簡單的表視圖,如圖01app

圖01atom

二、team.plistspa

<?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>

三、圖片code

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

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

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

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

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

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

Uruguay.png、USA.png

四、Main.storyboard,如圖02

圖02

五、.h

#import <UIKit/UIKit.h>

@interface CQ21ViewController : UITableViewController

@property (nonatomic, strong) NSArray *listTeams;

@end

六、.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
{
    // 一、加載cell
    static NSString *CellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    // 二、設置cell的textLabel
    NSUInteger row = [indexPath row];
    NSDictionary *rowDict = [self.listTeams objectAtIndex:row];
    cell.textLabel.text = [rowDict objectForKey:@"name"];
    
    // 三、設置cell的圖片
    NSString *imagePath = [rowDict objectForKey:@"image"];
    imagePath = [imagePath stringByAppendingString:@".png"];
    cell.imageView.image = [UIImage imageNamed:imagePath];
    
    // 四、設置cell擴展視圖爲擴展指示器
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

@end
相關文章
相關標籤/搜索