ITeye博客

ios8的tableView使用

haoningabc · 2014-10-18 20:34 · ios · 互联网 · iOS · Markdown

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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。