`
haoningabc
  • 浏览: 1465686 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

ios8的tableView使用

    博客分类:
  • ios
ios 
阅读更多
ios8是main.storyboard
不是原来的用file's owner绑定了
连线全练到View Controller就行了
参考
http://www.weheartswift.com/how-to-make-a-simple-table-view-with-ios-8-and-swift/

参考http://toyota2006.iteye.com/blog/841738
把autorelease设置成no



ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>{
    IBOutlet UITableView *tableViewList;
    NSMutableArray *dataItems;
}
@end

ViewController.m
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    dataItems=[[NSMutableArray alloc]initWithObjects:@"中国",@"美国",@"日本",nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{
    return [dataItems count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    NSUInteger row=[indexPath row];
    cell.textLabel.text=[dataItems objectAtIndex:row];
    return cell;
}
@end




加个自适应都输入文本输入框
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>{
    IBOutlet UITableView *tableViewList;
    NSMutableArray *dataItems;
    IBOutlet UITextField *talktxt;
}
@end

#import "ViewController.h"
@interface ViewController ()

@end
@implementation ViewController
int prewTag ;  //编辑上一个UITextField的TAG,需要在XIB文件中定义或者程序中添加,不能让两个控件的TAG相同
float prewMoveY; //编辑的时候移动的高度
- (void)viewDidLoad {
    [super viewDidLoad];
    //dataItems=[[NSMutableArray alloc]initWithObjects:@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",nil];
    talktxt.returnKeyType=UIReturnKeyDone;
    talktxt.delegate=self;
    
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    CGRect textFrame =  textField.frame;
    float textY = textFrame.origin.y+textFrame.size.height;
    float bottomY = self.view.frame.size.height-textY;
    if(bottomY>=216)  //判断当前的高度是否已经有216,如果超过了就不需要再移动主界面的View高度
    {
        prewTag = -1;
        return;
    }
    prewTag = textField.tag;
    float moveY = 216-bottomY;
    prewMoveY = moveY;
    
    NSTimeInterval animationDuration = 0.30f;
    CGRect frame = self.view.frame;
    frame.origin.y -=moveY;//view的Y轴上移
    frame.size.height +=moveY; //View的高度增加
    self.view.frame = frame;
    [UIView beginAnimations:@"ResizeView" context:nil];
    [UIView setAnimationDuration:animationDuration];
    self.view.frame = frame;
    [UIView commitAnimations];//设置调整界面的动画效果
}
-(void) textFieldDidEndEditing:(UITextField *)textField
{
    if(prewTag == -1) //当编辑的View不是需要移动的View
    {
        return;
    }
    float moveY ;
    NSTimeInterval animationDuration = 0.30f;
    CGRect frame = self.view.frame;
    if(prewTag == textField.tag) //当结束编辑的View的TAG是上次的就移动
    {   //还原界面
        moveY =  prewMoveY;
        frame.origin.y +=moveY;
        frame.size. height -=moveY;
        self.view.frame = frame;
    }
    //self.view移回原位置
    [UIView beginAnimations:@"ResizeView" context:nil];
    [UIView setAnimationDuration:animationDuration];
    self.view.frame = frame;
    [UIView commitAnimations];
    [textField resignFirstResponder];   
}
//键盘都return隐藏键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
 replacementText:(NSString *)text {
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}
//发送消息
- (IBAction)insertNew:(id)sender {
    if(!dataItems) {
        dataItems = [[NSMutableArray alloc] init];
    }
    [dataItems insertObject:talktxt.text atIndex:0];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [tableViewList insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    talktxt.text=nil;  
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    dataItems=nil;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section
{
    return [dataItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    NSUInteger row=[indexPath row];
    cell.textLabel.text=[dataItems objectAtIndex:row];
    return cell;
}
- (void)dealloc {
    [talktxt release];
    [super dealloc];
}
@end
  • 大小: 17.7 KB
  • 大小: 138.6 KB
分享到:
评论

相关推荐

    ios-tableView的多项选择删除.zip

    总的来说,"ios-tableView的多项选择删除.zip"项目涵盖了在iOS应用中实现tableView多选删除的核心步骤,包括开启多选模式、处理用户选择、展示选中状态以及实现删除功能。开发者可以根据自己的需求进行相应的扩展和...

    ios中tableview下拉更新效果例子

    "ios中tableview下拉更新效果例子"是一个典型的iOS应用功能,允许用户通过下拉刷新来获取最新的数据。这种功能常见于新闻应用、社交媒体应用等,用户可以随时获取到最新的信息。下面将详细讲解如何实现这个功能。 ...

    ios-tableview简单使用.zip

    在iOS开发中,UITableView是展示数据列表的重要控件,它被广泛用于应用的主界面、设置界面等场景。本教程将深入讲解如何简单地使用UITableView,包括设置UITableViewDataSource和UITableViewDelegate,以及它们中的...

    ios-tableview编辑.zip

    当我们谈论"ios-tableview编辑.zip"时,很显然这个压缩包可能包含了一系列关于如何实现UITableView的编辑和删除功能的代码示例、教程或资源。在iOS应用中,允许用户编辑和删除表格内容能提升用户体验,使他们能够更...

    ios-tableView的简单封装.zip

    这个名为“ios-tableView的简单封装.zip”的压缩包,显然提供了一个轻量级的UITableView封装库,名为QQtableView,作者通过GitHub分享了他的代码。从描述来看,该库主要实现了三大功能:下拉刷新、空页面处理以及...

    iOS8 tableview uitableviewcell内容自适应

    首先,我们要理解在iOS8之前,为了实现动态高度的TableViewCell,开发者通常需要手动计算每个单元格的高度,并在`tableView:heightForRowAtIndexPath:`代理方法中返回。这种方式繁琐且容易出错,特别是在内容多变的...

    IOS开发 tableview中cell的用法

    在iOS开发中,UITableView是一种非常常见的用户界面组件,用于展示一系列的数据列表,而UITableViewCell则是构成这个列表的基本单元,也就是我们常说的“单元格”。本文将深入探讨UITableView中cell的用法,包括数据...

    ios-tableVIew点击展开.zip

    在iOS开发中,UITableView是一种非常常见且重要的组件,用于展示数据列表。"tableView点击展开"这一功能通常是开发者为了实现更丰富的交互效果而设计的。它允许用户点击表格中的某一行,该行会像一个可折叠的菜单...

    ios-tableView使用

    本教程将深入讲解如何在iOS项目中有效地使用tableView,包括自定义和字典的使用。 首先,理解UITableView的基本结构至关重要。UITableView由多个UITableViewCell组成,每个cell代表一行数据。开发者需要定义cell的...

    ios-TableView-悬停.zip

    8. UICollectionView的使用:与UITableView类似,但提供了更丰富的布局和自定义选项。 9. UICollectionViewFlowLayout:默认布局,支持水平和垂直滚动,可以自定义间距、大小等。 10. UICollectionViewDelegate和...

    iOS-TableView入门示例

    这个入门示例将带你逐步了解如何在iOS应用中使用TableView,展示基础的数据,并进行更高级的定制。 首先,让我们了解一下UITableView的基本概念: 1. **UITableView**:它是苹果提供的一个类,用于创建和管理包含...

    iOS tableview里面嵌套tableview,自适应高度

    swift ,使用autolayout + storyboard 最外层tableview列表,cell里面嵌套了tableview,自适应内容,cell里的tableview不可滑动,内容全部显示,且文字分行显示,不用设置cell的高度直接可以自适应内容

    ios-TableView的Cell上播放视频.zip

    这个“ios-TableView的Cell上播放视频.zip”资源可能包含了一个示例项目,用于演示如何在UITableView的每个单元格(Cell)中播放视频。以下是对这个主题的详细说明: 首先,为了在UITableView的Cell中播放视频,...

    ios-tableView二级列表.zip

    总结,"ios-tableView二级列表.zip"项目展示了如何在iOS应用中实现一个交互式的、可扩展的两级列表,涉及到了UITableView的使用、数据模型设计、点击事件处理、自定义Cell和Header、动态计算高度等多个关键技术点。...

    ios-tableview 动态添加.zip

    "ios-tableview 动态添加.zip"这个压缩包很可能包含了一个示例项目,演示如何在运行时动态地向UITableView添加数据。在这里,我们将深入探讨在iOS应用中动态加载UITableView的相关知识点。 首先,UITableView的动态...

    ios-tableview下拉图片放大.zip

    总的来说,"ios-tableview下拉图片放大"项目涉及了UITableView的使用、视图动画的创建、滚动事件的监听以及性能优化等多个知识点。通过这个小功能,开发者可以提升用户的交互体验,使应用更具吸引力。同时,这也是对...

    ios-tableView定义倒计时显示.zip

    这个压缩包"ios-tableView定义倒计时显示.zip"包含了一个示例项目,它演示了如何在UITableView的单元格(cell)中添加倒计时功能。在这个教程中,开发者提供了一个基本的倒计时实现,但未涵盖获取毫秒级时间的方法。...

    ios-tableView 头部折叠.zip

    这个"ios-tableView 头部折叠.zip"项目显然涉及到一个特定的交互设计:当用户点击某个按钮时,TableView的头部会折叠或展开,以展示或隐藏额外的内容。这种效果通常用于节省屏幕空间并提供更丰富的用户体验。 首先...

    ios-tableview cell 倒计时.zip

    在“ios-tableview cell 倒计时.zip”这个项目中,我们聚焦于如何在UITableView的cell中实现倒计时功能。这个功能常见于许多应用,比如限时抢购、任务提醒等场景,能给用户带来实时感和紧迫感。 首先,我们需要了解...

    ios-tableview 嵌套 悬停.zip

    而“ios-tableview 嵌套 悬停”这个项目,显然关注的是如何在UITableView中实现嵌套的效果,并且具备悬停功能。这个项目来源于GitHub上的开源库`ArtScrollTableView`,由LeeWongSnail开发,提供了高级的滚动和悬停...

Global site tag (gtag.js) - Google Analytics