Iphone TableView 使用实现

来源:互联网 发布:javascript 预编译 编辑:程序博客网 时间:2024/05/01 07:02


TableView     加载不到数据

1、视图连线 没有指定数据源

2、视图指定代理类,也就是他的本身。

3、头文件继承 UIViewController<UITableViewDelegate,UITableViewDataSource>

头文件


#import <UIKit/UIKit.h>

@class ViewController;

@interface AllCoursesViewController :UIViewController<UITableViewDelegate,UITableViewDataSource>

{

    IBOutlet UITableView *table;

    IBOutlet UITextField *textField;

    IBOutlet UIButton *button;

    IBOutlet UILabel *nameTitleLable;

    //NSMutableArray *typeArray;

    NSMutableArray *contentArray;

    //int typeId;

    //NSMutableDictionary *dictionary;

    ViewController *mViewController;

    //IBOutlet UILabel *typeNameLabel;

    BOOL typeChooseHttp;

    UIView *typeView;

    BOOL typeViewSHow;

}

@property(nonatomic,retain)ViewController *mViewController;

-(void)checkDetailWith:(NSMutableDictionary *)dic;

-(IBAction)backView:(id)sender;

-(IBAction)textFiledDoneEditing:(id)sender;

-(IBAction)searchCourse:(id)sender;

-(void)choosetype:(id)sender;

-(IBAction)viewShow:(id)sender;

@end


实现文件



#import "ViewController.h"

#import "AllCoursesViewController.h"

#import "AllCoursesTableViewCell.h"

#import "ASIFormDataRequest.h"

#import "ConstDefine.h"

#import "JSON.h"

#import "UIHelp.h"

#import <QuartzCore/QuartzCore.h>

@interface AllCoursesViewController ()


@end



NSString* COURSE_TYPES[]={@"全部",@"心理学",@"数理",@"哲学",@"人文",@"经济",@"TED",@"生物学"};

NSString* COURSE_ICONS[]={@"item02.png",@"item05.png",@"item04.png", @"item06.png"

                          @"item03.png",@"item01.png",@"item08.png", @"shengwu.png"};


@implementation AllCoursesViewController

@synthesize mViewController;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}

-(IBAction)backView:(id)sender

{

    [mViewController.navigationControllerpopViewControllerAnimated:YES];

}

- (void)viewDidLoad

{

    [superviewDidLoad];

    // Do any additional setup after loading the view from its nib.

    //typeArray  = [[NSMutableArray alloc] init ];

    contentArray = [[NSMutableArrayalloc]init ];

    typeView.backgroundColor =[UIColorclearColor];

   // button = [UIButton buttonWithType:UIButtonTypeCustom];

    typeViewSHow = NO;

    if (typeViewSHow ==YES) {

        typeView.hidden =NO;

    [button setBackgroundImage:[UIImageimageNamed:@"3.png"]forState:UIControlStateNormal];

    }

    else

    {

    typeView.hidden =YES;

    [button setBackgroundImage:[UIImageimageNamed:@"2.png"]forState:UIControlStateNormal];

    }

    

    NSString *defaultCourseType = COURSE_TYPES[0];

    

    nameTitleLable.text = [NSStringstringWithFormat:@"%@课程", defaultCourseType];    

    

    typeView = [[UIViewalloc]init];

    typeView.backgroundColor = [UIColorgrayColor];

    UIImageView *backImage = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"更多课程.png" ]];

    [typeView addSubview:backImage];

    CGRect buttonRect = CGRectMake(24, 5, 51, 59);

    CGRect nameRect = CGRectMake(24, 65, 50, 14);

    int jiange  =23;

    int height;

    

    for (int i = 0; i < 8; i++) {

        UIButton* btn = [UIButtonbuttonWithType:UIButtonTypeCustom];

        UILabel *label = [[UILabelalloc]initWithFrame:nameRect];

        label.font =[UIFontfontWithName:FontNamesize:14];

        btn.frame = buttonRect;

        //NSDictionary *dic = [typeArray objectAtIndex:i];

       // int type = [[dic objectForKey:@"typeid"] integerValue];

        //[self imageWithUrl:[dic objectForKey:@"typepictureurl"] withButton:btn];

        UIImage *image = [UIImageimageNamed:COURSE_ICONS[i]];

        

        [btn setBackgroundImage:imageforState:UIControlStateNormal];

        btn.tag = i;

        [btn addTarget:selfaction:@selector(choosetype:)forControlEvents:UIControlEventTouchUpInside];

        [typeView addSubview:btn];

       // [typeView addSubview:label];

        

        if(i%4==3)

        {

            //height = nameRect.origin.y;

            buttonRect.origin.x =24, buttonRect.origin.y=buttonRect.origin.y+59;

        

           // nameRect.origin.x =24 , nameRect.origin.y=nameRect.origin.y+74;

            

        }

        else

        {

            buttonRect.origin.x+=buttonRect.size.width+jiange;

        }

    }

    backImage.frame = CGRectMake(0, 0, 320, buttonRect.origin.y+5);

    typeView.frame =CGRectMake(0, 44, 320, buttonRect.origin.y+5);


    [self.viewaddSubview:typeView];

    typeView.hidden =YES;



    [self connecthttp:defaultCourseType];

}

-(void)choosetype:(id)sender

{

    if (typeViewSHow ==NO) {

        typeViewSHow = YES;

    }

    else

        typeViewSHow = NO;

    if (typeViewSHow ==YES) {

        typeView.hidden =NO;

        [button setBackgroundImage:[UIImageimageNamed:@"3.png"]forState:UIControlStateNormal];

    }

    else

    {

        typeView.hidden =YES;

        [button setBackgroundImage:[UIImageimageNamed:@"2.png"]forState:UIControlStateNormal];

    }


    UIButton *clickedFuncButton = (UIButton *) sender;

 

    NSString *coursetype = COURSE_TYPES[clickedFuncButton.tag];

    nameTitleLable.text = [NSStringstringWithFormat:@"%@课程", coursetype];    

    //[UIHelp showHint:[[NSString alloc] initWithFormat:@"选择分类: %@", coursetype] intView:self.view];    

    [self connecthttp:coursetype];

}

-(void)imageWithUrl:(NSString*)stringurl  withButton:(UIButton*)btn

{

   

        dispatch_queue_t network;

        network =dispatch_queue_create("com.myapp.network",nil);

        dispatch_async(network, ^

                       {

                           NSData* data= [NSDatadataWithContentsOfURL:[NSURLURLWithString:stringurl]];

                           UIImage *mimage = [UIImageimageWithData:data];

                           dispatch_async(dispatch_get_main_queue(), ^{

                               [btn setBackgroundImage:mimageforState:UIControlStateNormal];

                           });

                           

                       });

    

}


-(void)connecthttp: (NSString *)coursetype

{

    typeView.hidden =YES;

    NSURL *url = [NSURLURLWithString:@"http://223.4.169.93:8080/microcourseapi/a=get_courses"];

    //ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];

    ASIFormDataRequest *asiRequest = [[ASIFormDataRequestalloc]initWithURL:url];

    [asiRequest setRequestMethod:@"POST"];

    [asiRequest addPostValue:HASHforKey:@"loginhashcode"];

    [asiRequest addPostValue:coursetype forKey:@"coursetype"];

    

    [asiRequest buildPostBody];

    [asiRequest setDelegate:self];

    [asiRequest startSynchronous];

    

    NSData *responseData = [asiRequest responseData];

    NSString* returnString = [[NSStringalloc]initWithData:responseData encoding:NSUTF8StringEncoding];

    

    SBJsonParser *parser = [[SBJsonParseralloc]init];

NSDictionary *result = [parserobjectWithString:returnString];

    if ([[result objectForKey:@"code"]intValue]==0) {

        // 清空课程列表

        [contentArray removeAllObjects];

        [contentArray addObjectsFromArray:[result objectForKey:@"list"]];

        NSLog(@"%d",[contentArraycount]);

    }

    else {

        //  [UIHelp showHint:[result objectForKey:@"msg"] intView:self.view];

    }

    [table reloadData];

    [asiRequest release];

    [parser release];

}

  

-(IBAction)viewShow:(id)sender

{

    if (typeViewSHow ==NO) {

        typeViewSHow = YES;

    }

    else

        typeViewSHow = NO;

    if (typeViewSHow ==YES) {

        typeView.hidden =NO;

        [button setBackgroundImage:[UIImageimageNamed:@"3.png"]forState:UIControlStateNormal];

    }

    else

    {

        typeView.hidden =YES;

        [button setBackgroundImage:[UIImageimageNamed:@"2.png"]forState:UIControlStateNormal];

    }

}


 


- (void)viewDidUnload

{

    [superviewDidUnload];

    // Release any retained subviews of the main view.

    // e.g. self.myOutlet = nil;

}

// 多少个组参考iphone手机上的设置


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    //这个方法用来告诉表格有几个分组

    return 1;

}


//多少行 类似androd 返回总数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    NSLog(@"%d",[contentArraycount]);

        return [contentArraycount];

}


//显示每行视图


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    

    static NSString *CellIdentifier = @"CustomCellIdentifier";

    AllCoursesTableViewCell *cell = (AllCoursesTableViewCell *)[tableViewdequeueReusableCellWithIdentifier:CellIdentifier];

    

   // if (cell == nil) {

        cell = [[[AllCoursesTableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:CellIdentifier]autorelease];

//        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"AllCoursesTableViewCell" owner:self options:nil];

//        cell = [array objectAtIndex:0];

    cell.mViewController =self;

    UIImageView *image = [[UIImageViewalloc]initWithFrame:cell.frame];

    image.image= [UIImageimageNamed:@"课程选中框.png"];

    cell.selectedBackgroundView = image;

    [image release];

    

    NSMutableDictionary *dic = [contentArrayobjectAtIndex:indexPath.row];

//    NSLog(@"%@",dic);

    [cell changeDataWith:dic];

//

//   //  }

//    

    return cell;

}

-(IBAction)textFiledDoneEditing:(id)sender

{

    [sender resignFirstResponder];

}

-(IBAction)searchCourse:(id)sender

{

    [textFieldresignFirstResponder];

    if (textField.text==nil||[textField.textlength]==0)

    {

        [UIHelpshowHint:@"请输入关键字!" intView:self.view];

        return;

    }

    NSURL *url = [NSURLURLWithString:@"http://223.4.169.93:8080/microcourseapi/a=search_courses"];

    //ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];

    ASIFormDataRequest *asiRequest = [[ASIFormDataRequestalloc]initWithURL:url];

    [asiRequest setRequestMethod:@"POST"];

    [asiRequest addPostValue:HASH forKey:@"loginhashcode"];

    [asiRequest addPostValue:textField.textforKey:@"keyword"];

    

    [asiRequest buildPostBody];

    [asiRequest setDelegate:self];

    [asiRequest startSynchronous];

    

    NSData *responseData = [asiRequestresponseData];

    NSString* returnString = [[NSStringalloc]initWithData:responseData encoding:NSUTF8StringEncoding];

    

    SBJsonParser *parser = [[SBJsonParseralloc]init];

NSDictionary *result = [parserobjectWithString:returnString];

    if ([[resultobjectForKey:@"code"]intValue]==0) {

        [contentArrayremoveAllObjects];

        [contentArrayaddObjectsFromArray:[resultobjectForKey:@"list"]];

        NSLog(@"%d",[contentArraycount]);

        [tablereloadData];

    }

    else {

        [UIHelpshowHint:[resultobjectForKey:@"msg"]intView:self.view];

    }

    

    [asiRequest release];

    [parser release];


}


//设置每行的行高

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{              

    return 110.0;

}


//选中某行时,作出响应

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

    

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

    int row = [indexPathrow];

    NSMutableDictionary *dic = [contentArrayobjectAtIndex:row];

    [selfcheckDetailWith:dic];

    

}

-(void)checkDetailWith:(NSMutableDictionary *)dic

{

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    returnNO;//(interfaceOrientation == UIInterfaceOrientationPortrait);

}


@end



原创粉丝点击