iPhone第三节:UITableView

UITableView

1、UITableView

2、UITableViewController

3、UITableViewCell



----练习----

Mode==Record

#import <Foundation/Foundation.h>

@interface Record : NSObject

@property (nonatomic, strong) NSString * headImageName;     //头像图片名称
@property (nonatomic, strong) NSString * userNameText;      //用户名文本
@property (nonatomic, strong) NSString * heightBMIText;     //体重指数BMI文本
@property (nonatomic, strong) NSString * contentText;       //内容文本
@property (nonatomic, strong) NSString * contentImageName1; //内容图片名称
@property (nonatomic, strong) NSString * contentImageName2;
@property (nonatomic, strong) NSString * contentImageName3;
@property (nonatomic, strong) NSString * timeText;          //时间文本
@property (nonatomic, strong) NSString * heartImageName;    //心图片名称
@property (nonatomic, assign) long heartText;         //心文本
@property (nonatomic, strong) NSString * commentImageName;  //评论图片名称
@property (nonatomic, strong) NSString * commentText;       //评论文本

//初始化方法
- (id)initWithHeadImageName:(NSString *)head andUserNameText:(NSString *)user andHeightBMIText:(NSString *)height andContentText:(NSString *)content andContentImageName1:(NSString *)contentImage1 andContentImageName2:(NSString *)contentImage2 andContentImageName3:(NSString *)contentImage3 andTimeText:(NSString *)time andHeartImageName:(NSString *)heart andHeartText:(long)heartText andCommentImageName:(NSString *)comment andCommentText:(NSString *)commentText;

@end
#import "Record.h"

@implementation Record

- (id)initWithHeadImageName:(NSString *)head andUserNameText:(NSString *)user andHeightBMIText:(NSString *)height andContentText:(NSString *)content andContentImageName1:(NSString *)contentImage1 andContentImageName2:(NSString *)contentImage2 andContentImageName3:(NSString *)contentImage3 andTimeText:(NSString *)time andHeartImageName:(NSString *)heart andHeartText:(long)heartText andCommentImageName:(NSString *)comment andCommentText:(NSString *)commentText
{
    if (self = [super init])
    {
        _headImageName = head;
        _userNameText = user;
        _heightBMIText = height;
        _contentText = content;
        _contentImageName1 = contentImage1;
        _contentImageName2 = contentImage2;
        _contentImageName3 = contentImage3;
        _timeText = time;
        _heartImageName = heart;
        _heartText = heartText;
        _commentImageName = comment;
        _commentText = commentText;
    }
    return self;
}

@end
View==RecordTableViewCell

#import <UIKit/UIKit.h>

@interface RecordTableViewCell : UITableViewCell

@property (nonatomic, strong) UIImageView * headImageView;     //头像图片视图
@property (nonatomic, strong) UILabel * userNameLabel;         //用户名标签
@property (nonatomic, strong) UILabel * heightBMILabel;        //体重指数BMI标签
@property (nonatomic, strong) UILabel * contentLabel;          //内容标签
@property (nonatomic, strong) UIImageView * contentImageView1; //内容图片视图
@property (nonatomic, strong) UIImageView * contentImageView2;
@property (nonatomic, strong) UIImageView * contentImageView3;
@property (nonatomic, strong) UILabel * timeLabel;             //时间标签
@property (nonatomic, strong) UIButton * heartImageButton;     //心图片按钮
@property (nonatomic, strong) UILabel * heartLabel;            //心标签
@property (nonatomic, strong) UIButton * commentImageButton;   //评论图片按钮
@property (nonatomic, strong) UILabel * commentLabel;          //评论标签
@property (nonatomic, strong) UIScrollView * contentImageSV;   //滚动视图

@end
#import "RecordTableViewCell.h"
#import "QuartzCore/QuartzCore.h"
#import "GlobalDefine.h"

@implementation RecordTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        //头像
        _headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(12.5, 11, 54.5, 54.5)];
        //圆角view
        _headImageView.layer.cornerRadius = 27.25;//(像素一半,为圆形,小于一半,为原图形圆角,数值越大圆角越大)
        _headImageView.layer.masksToBounds = YES;
        [self.contentView addSubview:_headImageView];
        
        //用户名
        _userNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(12.5, 75, 54.5, 20)];
        _userNameLabel.font = [UIFont boldSystemFontOfSize:12.0];
        _userNameLabel.textColor = [UIColor blackColor];
        _userNameLabel.textAlignment = NSTextAlignmentCenter;
        _userNameLabel.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:_userNameLabel];
        
        //体重指数
        _heightBMILabel = [[UILabel alloc] initWithFrame:CGRectMake(92, 11, 150, 20)];
        _heightBMILabel.font = [UIFont boldSystemFontOfSize:16.0];
        //_heightBMILabel.textColor = [UIColor colorWithRed:69 green:9d blue:d1 alpha:1];
        _heightBMILabel.textColor = [UIColor blueColor];
        _heightBMILabel.textAlignment = NSTextAlignmentLeft;
        _heightBMILabel.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:_heightBMILabel];
        
        //内容
        _contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(92, 42, 215, 20)];
        _contentLabel.font = [UIFont systemFontOfSize:15.0];
        _contentLabel.textColor = [UIColor grayColor];
        _contentLabel.textAlignment = NSTextAlignmentLeft;
        _contentLabel.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:_contentLabel];
        
        //非滚动视图的内容图片
//        _contentImageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(92, 77, 40, 40)];
//        _contentImageView1.layer.cornerRadius = 5;
//        _contentImageView1.layer.masksToBounds = YES;
//        [self.contentView addSubview:_contentImageView1];
//        
//        _contentImageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(137, 77, 40, 40)];
//        _contentImageView2.layer.cornerRadius = 5;
//        _contentImageView2.layer.masksToBounds = YES;
//        [self.contentView addSubview:_contentImageView2];
//        
//        _contentImageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(182, 77, 40, 40)];
//        _contentImageView3.layer.cornerRadius = 5;
//        _contentImageView3.layer.masksToBounds = YES;
//        [self.contentView addSubview:_contentImageView3];
        
        //时间
        _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCWIDTH - 12.5 - 60, 15, 60, 16)];
        _timeLabel.font = [UIFont systemFontOfSize:12.0];
        _timeLabel.textColor = [UIColor redColor];
        _timeLabel.textAlignment = NSTextAlignmentRight;
        _timeLabel.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:_timeLabel];
        
        //心按钮
        _heartImageButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _heartImageButton.frame = CGRectMake(192, 130, 15, 12.5);
        _heartImageButton.showsTouchWhenHighlighted = YES;
//        [_heartImageButton addTarget:self action:@selector(heart) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_heartImageButton];
        
        //评论按钮
        _commentImageButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _commentImageButton.frame = CGRectMake(SCWIDTH - 50, 130, 15, 12.5);
        _commentImageButton.showsTouchWhenHighlighted = YES;
//        [_commentImageButton addTarget:self action:@selector(comment) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_commentImageButton];
        
        //心标签
        _heartLabel = [[UILabel alloc] initWithFrame:CGRectMake(210, 130, 13, 13)];
        _heartLabel.font = [UIFont systemFontOfSize:11.0];
        _heartLabel.textColor = [UIColor grayColor];
        _heartLabel.textAlignment = NSTextAlignmentLeft;
        _heartLabel.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:_heartLabel];
        
        //评论标签
        _commentLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCWIDTH - 32, 130, 13, 13)];
        _commentLabel.font = [UIFont systemFontOfSize:11.0];
        _commentLabel.textColor = [UIColor grayColor];
        _commentLabel.textAlignment = NSTextAlignmentLeft;
        _commentLabel.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:_commentLabel];
     
        //滚动视图的内容图片
        _contentImageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
        _contentImageView1.layer.cornerRadius = 5;
        _contentImageView1.layer.masksToBounds = YES;
        [_contentImageSV addSubview:_contentImageView1];
        
        _contentImageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(45, 0, 40, 40)];
        _contentImageView2.layer.cornerRadius = 5;
        _contentImageView2.layer.masksToBounds = YES;
        [_contentImageSV addSubview:_contentImageView2];
        
        _contentImageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(90, 0, 40, 40)];
        _contentImageView3.layer.cornerRadius = 5;
        _contentImageView3.layer.masksToBounds = YES;
        [_contentImageSV addSubview:_contentImageView3];
        
        //滚动视图
        _contentImageSV = [[UIScrollView alloc] initWithFrame:CGRectMake(92, 77, 130, 40)];
        _contentImageSV.pagingEnabled = YES;
        _contentImageSV.bounces = NO;
        _contentImageSV.contentSize = CGSizeMake(260, 40);
        [self.contentView addSubview:_contentImageSV];
    }
    return self;
}

//- (void)heart
//{
//    _heartLabel.text = (long)_heartLabel.text;
//    
//}

//点击评论的响应事件
//- (void)comment
//{
//    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"评论请点击确定" message:nil delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
//    [alert show];
//    //当滑回来的时候,数值就变回原值了
    long longComment = [_commentLabel.text longLongValue];
    longComment++;
    _commentLabel.text = [NSString stringWithFormat:@"%ld", longComment];
//}

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
Controller==RecordViewController

#import <UIKit/UIKit.h>
//全局,传值
//NSIndexPath * _selectedIndexPath; //选中的位置
//NSMutableArray * modelArray;      //存放模型的数组

@interface RecordViewController : UIViewController

{
    NSIndexPath * _selectedIndexPath; //选中的位置
    NSMutableArray * modelArray;      //存放模型的数组
}

@property (nonatomic, strong) UITableView * tableViewRecord;

@end
#import "RecordViewController.h"
#import "RecordTableViewCell.h"
#import "Record.h"
#import "GlobalDefine.h"
#import "ImageViewController.h"

@interface RecordViewController () <UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate>

{
    NSArray * userNameArray;  //存放用户名
    NSArray * contentArray;   //存放内容
    BOOL isDone ; //保存点赞状态
}

@end

@implementation RecordViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    //白色背景
    self.view.backgroundColor = [UIColor whiteColor];
    //初始化TableView
    [self initTableView];
    //创建模型数组
    modelArray = [[NSMutableArray alloc] initWithCapacity:1];
    //用户名和内容
    userNameArray = [NSArray arrayWithObjects:@"小红同学", @"小橙同学", @"小黄同学", @"小绿同学", @"小青同学", @"小蓝同学", @"小紫同学", @"小黑同学", @"小白同学", @"小丫同学", nil];
    contentArray = [NSArray arrayWithObjects:@"今天的风儿", @"好喧嚣啊~", @"这风儿", @"似有些略略鸣泣。", @"风儿把不祥的东西", @"吹到镇子里去了。", @"快走吧,", @"在风停止之前...", @"喂!不得了啦!", @"那边的薯片半价啊!", nil];
    //循环初始化模型并放入模型数组
    for (int i = 0; i < 10; i++)
    {
        Record * recordModel = [[Record alloc] initWithHeadImageName:[NSString stringWithFormat:@"headImage%d.jpg", i] andUserNameText:userNameArray[i] andHeightBMIText:[NSString stringWithFormat:@"体重指数  BMI: %d", i * 2] andContentText:contentArray[i] andContentImageName1:[NSString stringWithFormat:@"contentImage%d.jpg", i % 8] andContentImageName2:[NSString stringWithFormat:@"contentImage%d.jpg", i % 8 + 1] andContentImageName3:[NSString stringWithFormat:@"contentImage%d.jpg", i % 8 + 2] andTimeText:[NSString stringWithFormat:@"%d小时前", i+ 1] andHeartImageName:@"heart.png" andHeartText:(@"%d", i * 10) andCommentImageName:@"comment.png" andCommentText:[NSString stringWithFormat:@"%d", i * 10]];
        [modelArray addObject:recordModel];
    }
}

//初始化
- (void)initTableView
{
    //位置、大小,风格
    _tableViewRecord = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCWIDTH, SCHEIGHT) style:UITableViewStylePlain];
    //添加子视图
    [self.view addSubview:_tableViewRecord];
    //设置代理
    _tableViewRecord.dataSource = self;
    _tableViewRecord.delegate = self;
}

//返回行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [modelArray count];
}

//每行单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //缓存池标识
    NSString * cellId = @"reuseCellId";
    //到缓存池寻找cell
    RecordTableViewCell * recordCell = [tableView dequeueReusableCellWithIdentifier:cellId];
    //如果没有则新建,并放入缓存池
    if (!recordCell)
    {
        recordCell = [[RecordTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    //当前行的对象模型
    Record * model = [modelArray objectAtIndex:indexPath.row];
    //分别赋值
    recordCell.headImageView.image = [UIImage imageNamed:model.headImageName];
    recordCell.userNameLabel.text = model.userNameText;
    //同一label不同颜色显示
    //取得要改变颜色的字符串的范围
    NSRange range = [model.heightBMIText rangeOfString: @"体重指数"];
    //新建可变的属性化字符串,并用model的heightBMIText初始化。
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString: model.heightBMIText];
    //为属性化字符串的特定范围添加属性
    [attrString addAttributes: @{NSForegroundColorAttributeName: [UIColor blackColor]} range: range];
    recordCell.heightBMILabel.attributedText = attrString;
    
    recordCell.contentLabel.text = model.contentText;
    
    recordCell.contentImageView1.image = [UIImage imageNamed:model.contentImageName1];
    recordCell.contentImageView2.image = [UIImage imageNamed:model.contentImageName2];
    recordCell.contentImageView3.image = [UIImage imageNamed:model.contentImageName3];
    //赋值后的内容图片加入滚动视图 (不加则不显示图片)
    [recordCell.contentImageSV addSubview:recordCell.contentImageView1];
    [recordCell.contentImageSV addSubview:recordCell.contentImageView2];
    [recordCell.contentImageSV addSubview:recordCell.contentImageView3];
    [recordCell.contentImageSV addSubview:recordCell.contentImageView1];
    
    recordCell.timeLabel.text = model.timeText;
    [recordCell.heartImageButton setBackgroundImage:[UIImage imageNamed:model.heartImageName] forState:UIControlStateNormal];
    [recordCell.heartImageButton addTarget:self action:@selector(heart:) forControlEvents:UIControlEventTouchUpInside];
    [recordCell.commentImageButton addTarget:self action:@selector(comment) forControlEvents:UIControlEventTouchUpInside];
    recordCell.heartImageButton.tag = indexPath.row;
    recordCell.heartLabel.text = [NSString stringWithFormat:@"%ld", model.heartText ];
    [recordCell.commentImageButton setBackgroundImage:[UIImage imageNamed:model.commentImageName] forState:UIControlStateNormal];
    recordCell.commentLabel.text = model.commentText;

    return recordCell;
}

//点赞,数字的label
- (void)heart:(UIButton *)sender
{
    //使用tag可以使点击的按钮所在行label增加
    //根据tag获得按钮所在行的模型
    Record * model = [modelArray objectAtIndex:sender.tag];
    model.heartText++;
    //需要局部刷新的单元格的组、行,放入数组
    NSIndexPath * selectedPath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
    NSArray *indexPaths = @[selectedPath];
    [_tableViewRecord reloadRowsAtIndexPaths:indexPaths withRowAnimation:NO];
    
#warning 没有实现点击第二次提示
    //实现只点一次,点击第二次弹出提示  = = 并没有实现
//    UIButton * newBtn = (UIButton *)[self.view viewWithTag:sender.tag];
//    if (newBtn.selected)
//    {
//        model.heartText++;
//        newBtn.selected = YES;
//        NSIndexPath * selectedPath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
//        NSArray *indexPaths = @[selectedPath];//需要局部刷新的单元格的组、行,放入数组
//        [_tableViewRecord reloadRowsAtIndexPaths:indexPaths withRowAnimation:NO];
//    }
//    else
//    {
//        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"你已经赞过了" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
//        [alert show];
//        [self performSelector:@selector(dismiss:) withObject:alert afterDelay:1.0];
//    }
}

- (void)dismiss:(UIAlertView *)alertView
{
    if (alertView)
    {
        [alertView dismissWithClickedButtonIndex:[alertView cancelButtonIndex] animated:NO];
    }
}
//评论,字符串的label
- (void)comment
{
    //而不使用tag则是使选中行的label增加(无论按钮在哪一行)
    //根据选中行获得该行的模型
    Record * model = [modelArray objectAtIndex:_selectedIndexPath.row];
    //将模型的评论文本字符串转为float类型(long也可以,但是只能转为longLongValue)
    float comment = [model.commentText floatValue];
    comment++;
    //将float类型转为字符串为评论文本赋值(注意去掉小数点)
    model.commentText = [NSString stringWithFormat:@"%.0f", comment];
    //需要局部刷新的单元格的组、行,放入数组
    NSArray *indexPaths = @[_selectedIndexPath];
    [_tableViewRecord reloadRowsAtIndexPaths:indexPaths withRowAnimation:NO];
}

//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150.0;
}

//点击行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //为点击的位置_selectedIndexPath赋值
    _selectedIndexPath = indexPath;
    //实例化一个记录,该记录为模型数组中点击的行的模型
    Record * recordModel = modelArray[indexPath.row];
    //创建弹出窗口
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"提示" message:recordModel.contentText delegate:self cancelButtonTitle:@"查看图片" otherButtonTitles:@"编辑文本", nil];
    //设置窗口风格,普通的文本输入(默认、秘文输入、账号密码输入)
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    //创建文本框并用提示的文本框赋值
    UITextField * textField = [alert textFieldAtIndex:0];
    //设置文本框内容,为记录模型的内容文本
    textField.text = recordModel.contentText;
    [alert show];
}
//窗口代理方法,用户保存数据、跳转
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1)  //当点击第二个按钮(编辑文本)
    {
        //创建文本框并用提示的文本框赋值
        UITextField * textField = [alertView textFieldAtIndex:0];
        //实例化一个记录,该记录为模型数组中点击的行的模型
        Record * recordModel = modelArray[_selectedIndexPath.row];
        //设置该记录模型的内容文本,为输入的内容(文本框的文本)
        recordModel.contentText = textField.text;
        //[_tableViewRecord reloadData];  //刷新整个view
        //刷新表格
        NSArray *indexPaths = @[_selectedIndexPath];//需要局部刷新的单元格的组、行,放入数组
        [_tableViewRecord reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationLeft];//后面的参数代表更新时的动画
    }
    else if (buttonIndex == 0)  //(查看图片)
    {
        //新建控制器
        ImageViewController * imageVC = [[ImageViewController alloc] init];
        //跳转风格(水平翻转)
        imageVC.modalTransitionStyle = 1;
        //在这里为第二个视图组合的model赋值,为模型数组中选中行的模型
        imageVC.model = [modelArray objectAtIndex:_selectedIndexPath.row];
        [self presentViewController:imageVC animated:YES completion:nil];
        
    }
}
//重写状态栏方法
- (UIStatusBarStyle)preferredStatusBarStyle
{
    //状态栏默认黑色,适用于浅色背景,更改为LightContent(白色,与背景同色),避免影响界面显示
    return UIStatusBarStyleLightContent;
}


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

@end
Controller2==ImageViewController

#import <UIKit/UIKit.h>
#import "Record.h"  //组合

@interface ImageViewController : UIViewController

//页数标签
@property (nonatomic, strong) UILabel *labelTop;
@property (nonatomic, strong) Record * model;  //组合

@end
#import "ImageViewController.h"
#import "RecordViewController.h"
#import "GlobalDefine.h"

@interface ImageViewController ()<UIScrollViewDelegate>

@end

@implementation ImageViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
//自动消失
- (void)dismiss:(UIAlertView *) alertView
{
    if (alertView)
    {
        [alertView dismissWithClickedButtonIndex:[alertView cancelButtonIndex] animated:YES];
    }
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    //提示,并于1.5s后自动消失
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"欢迎" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
    [alert show];
    [self performSelector:@selector(dismiss:) withObject:alert afterDelay:1.5];
    
    //实例化一个记录模型,其为模型数组中选中行的模型(全局,非全局则不需要)
    //Record * model = [modelArray objectAtIndex:_selectedIndexPath.row];
    
    //新建滚动视图,全屏,分页,无弹性
    UIScrollView * imageSV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCWIDTH, SCHEIGHT)];
    imageSV.pagingEnabled = YES;
    imageSV.bounces = NO;
    
    //滚动视图的图片视图,居中显示,正方形
    UIImageView * imageV1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, (SCHEIGHT - SCWIDTH) / 2, SCWIDTH, SCWIDTH)];
    //取出模型中的内容图片名,给图片视图的图片赋值(非全局用_model,全局model,以区分)
    imageV1.image = [UIImage imageNamed:_model.contentImageName1];
    UIImageView * imageV2 = [[UIImageView alloc] initWithFrame:CGRectMake(SCWIDTH, (SCHEIGHT - SCWIDTH) / 2, SCWIDTH, SCWIDTH)];
    imageV2.image = [UIImage imageNamed:_model.contentImageName2];
    UIImageView * imageV3 = [[UIImageView alloc] initWithFrame:CGRectMake(SCWIDTH * 2, (SCHEIGHT - SCWIDTH) / 2, SCWIDTH, SCWIDTH)];
    imageV3.image = [UIImage imageNamed:_model.contentImageName3];
    //图片视图加入滚动视图的子视图
    [imageSV addSubview:imageV1];
    [imageSV addSubview:imageV2];
    [imageSV addSubview:imageV3];
    
    //扩展滚动视图的大小,设置代理
    imageSV.contentSize = CGSizeMake(SCWIDTH * 3, SCHEIGHT);
    imageSV.delegate = self;
    [self.view addSubview:imageSV];
    
    //页数标签
#pragma mark ==labelTop
    _labelTop = [[UILabel alloc] initWithFrame:CGRectMake(120, 20, 80, 25)];
    _labelTop.text = [NSString stringWithFormat:@"当前页1/3"];
    _labelTop.backgroundColor = [UIColor blackColor];
    _labelTop.font = [UIFont systemFontOfSize:16.0];
    _labelTop.textAlignment = NSTextAlignmentCenter;
    _labelTop.textColor = [UIColor whiteColor];
    [self.view addSubview:_labelTop];
    
    //返回按钮
#pragma mark ==buttonBack
    UIButton *buttonBack = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonBack.frame = CGRectMake(220, SCHEIGHT - 50 , 50, 30);
    [buttonBack setTitle:@"返回" forState:UIControlStateNormal];
    [buttonBack setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [buttonBack setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [buttonBack addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonBack];
}

//返回的响应事件
- (void)back
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

//更改页数标签
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //分页模式,滑完,不分页模式滑到滑到2分之1改变label(需点击)
    int currentPage = (scrollView.contentOffset.x - SCWIDTH / 2) / SCWIDTH + 1;
    //分页模式,滑完这一页改变label
    //int currentPage = scrollView.contentOffset.y / SCHEIGHT;
    _labelTop.text =[NSString stringWithFormat:@"当前页%d/3", currentPage + 1 ];
}

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

@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值