【纯代码】TableView简单配置

1.做好准备:引入包,声明等。

<span style="font-size:24px;">#import "EquipTypeListViewController.h"
#import "EquipNetworking.h"//网络获取
#import "GeneralConfig.h"//通用配置
#import "UIDevice-Hardware.h"//检测设备硬件
#import "EquipListViewController.h"</span><pre name="code" class="objc"><span style="font-size:24px;">static NSString *const cellIdentify = @"EquipType";

@interface EquipTypeListViewController ()

@property (nonatomic, strong) NSArray *equipArray;

@end</span>

 
 



2.把网络获取需要的资源保存到当前类,再初始化TableView

    [EquipNetworking getEquipTypeList:^(NSArray *equipList, NSError *error) {
        self.equipArray = equipList;
        
        [self initEquipTypeListTableView];
    }];



3.设置TableView页面位置大小代理分割线

- (void)initEquipTypeListTableView{
    UITableView *equipTypeListTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, P_WIDTH, P_HEIGHT-64)];
    //判断iOS7.0之后tableview的分割线偏移
    if ([[[UIDevice currentDevice] systemVersion] intValue] > 7.0) {
        equipTypeListTable.separatorInset = UIEdgeInsetsZero;
        equipTypeListTable.layoutMargins  = UIEdgeInsetsZero;
    }
    equipTypeListTable.delegate = self;
    equipTypeListTable.dataSource = self;
    [self.view addSubview:equipTypeListTable];
}
设置分割线需要在TableView里设置

 equipTypeListTable.separatorInset = UIEdgeInsetsZero;

 equipTypeListTable.layoutMargins  = UIEdgeInsetsZero;

TableViewCell里设置

cell.layoutMargins  = UIEdgeInsetsZero;



4.在代理方法中设置行数以及每行内容

#pragma mark - UITableView的相关代理方法

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.equipArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify];
    }
    //判断iOS7.0之后tableview的分割线偏移
    if ([[[UIDevice currentDevice] systemVersion] intValue] > 7.0) {

        cell.layoutMargins  = UIEdgeInsetsZero;
    }
    cell.textLabel.text = [[self.equipArray objectAtIndex:indexPath.row] valueForKey:@"text"];
   
    return cell;
}

5.需要点击进入后实现功能的话就调用代理方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{//取消选中某一行(去掉cell选中时默认的蓝色背景)

[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];

}


下面是全部的代码:

//
//  EquipTypeListViewController.m
//  LoLBox_New
//
//  Created by ceshi on 14/12/22.
//  Copyright (c) 2014年 ceshi. All rights reserved.
//

#import "EquipTypeListViewController.h"
#import "EquipNetworking.h"
#import "GeneralConfig.h"
#import "UIDevice-Hardware.h"
#import "EquipListViewController.h"

static NSString *const cellIdentify = @"EquipType";

@interface EquipTypeListViewController ()

@property (nonatomic, strong) NSArray *equipArray;

@end

@implementation EquipTypeListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configTitleAndBackItem:nil];
    self.title = @"装备分类";
    
    [EquipNetworking getEquipTypeList:^(NSArray *equipList, NSError *error) {
        self.equipArray = equipList;
        
        [self initEquipTypeListTableView];
    }];
    // Do any additional setup after loading the view.
}

- (void)initEquipTypeListTableView{
    UITableView *equipTypeListTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, P_WIDTH, P_HEIGHT-64)];
    //判断iOS7.0之后tableview的分割线偏移
    if ([[[UIDevice currentDevice] systemVersion] intValue] > 7.0) {
        equipTypeListTable.separatorInset = UIEdgeInsetsZero;
        equipTypeListTable.layoutMargins  = UIEdgeInsetsZero;
    }
    equipTypeListTable.delegate = self;
    equipTypeListTable.dataSource = self;
    [self.view addSubview:equipTypeListTable];
}


#pragma mark - UITableView的相关代理方法

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.equipArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify];
    }
    //判断iOS7.0之后tableview的分割线偏移
    if ([[[UIDevice currentDevice] systemVersion] intValue] > 7.0) {
//        cell.separatorInset = UIEdgeInsetsZero;
        cell.layoutMargins  = UIEdgeInsetsZero;
    }
    cell.textLabel.text = [[self.equipArray objectAtIndex:indexPath.row] valueForKey:@"text"];
   
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
    EquipListViewController *equipList = [[EquipListViewController alloc] initWithTag:[[self.equipArray objectAtIndex:indexPath.row] valueForKey:@"tag"] title:[[self.equipArray objectAtIndex:indexPath.row] valueForKey:@"text"]];
    [self.navigationController pushViewController:equipList animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值