疑问: NSArray 访问问题

.h文件

#import
<UIKit/UIKit.h> #import "InterfaceHelper.h" #import "QuartzCore/QuartzCore.h" @interface FanWeiandLeiXingViewController : UIViewController<UITableViewDelegate,UITableViewDataSource> { IBOutlet UILabel *labTitle; IBOutlet UILabel *labSubTitle; NSArray *arrFanWei; NSArray *arrLeiXing; NSString *strOriginal; NSMutableDictionary *dic_tabelViewDataSource; UITableView *commonTableView; NSInteger rowCount; // tableview 的行数 范围和类型两个表共用 } @property(nonatomic,retain)NSString *strBtnName; // 标记那个按钮被按下 @property(nonatomic,retain)NSString *strTypeName; //标记模块的名字 @property(nonatomic,retain)NSArray *arrLeiXing; //问题根源,如果没有这句,.m文件中,除了viewDidLoad 方法外,其它方法不能访问arrLeiXing 变量。也就是说,只能用self.的方式来访问这个数组对象。难道全局对象,必须都用self.的方式来访问吗?
-(IBAction)btnBack:(id)sender; -(IBAction)btnSubmitClicked:(id)sender; //确定搜索按钮 //将json 格式的原始数据转解析成数据字典 -(NSMutableDictionary *)dictionaryFromJsonFormatOriginalData:(NSString *)strOriginal; // 将带有去除json数据中的转义字符 -(NSString *)removeEscapeCodeFromString:(NSString *)str; @end

.m文件

#import "FanWeiandLeiXingViewController.h"

@interface FanWeiandLeiXingViewController ()

@end

@implementation FanWeiandLeiXingViewController
@synthesize strBtnName;
@synthesize strTypeName;
@synthesize arrLeiXing;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Do any additional setup after loading the view from its nib.
    //初始化
    arrFanWei = [NSMutableArray alloc];
    self.arrLeiXing = [[NSArray alloc]init];
    rowCount = 0;
    
    /*
       获取需要显示的数据
     */
    if ([self.strBtnName isEqualToString:@"FanWei"]) {
        NSString *strTemp = [InterfaceHelper getSearchRange];
        NSLog(@"strTemp=====%@",strTemp);
        arrFanWei = [strTemp componentsSeparatedByString:@","];
        NSLog(@"arrFanWei.count====%d",arrFanWei.count);
        rowCount = arrFanWei.count;
    }
    if ([self.strBtnName isEqualToString:@"LeiXing"]) {
        labTitle.text = @"类型筛选";
        labSubTitle.text = @"类型:";
        if ([self.strTypeName isEqualToString:@"QuanChengJingDian"]) {
            strOriginal = [InterfaceHelper getHotelETCSecondLevelDirectoryByTypeId:@"2"];
        }else if([self.strTypeName isEqualToString:@"QuanChengZhuSu"]){
            strOriginal = [InterfaceHelper getHotelETCSecondLevelDirectoryByTypeId:@"1"];
        }else if ([self.strTypeName isEqualToString:@"YuLe"]) {
            strOriginal = [InterfaceHelper getHotelETCSecondLevelDirectoryByTypeId:@"5"];
        }else if ([self.strTypeName isEqualToString:@"JiNanMeiShi"]) {
            strOriginal = [InterfaceHelper getHotelETCSecondLevelDirectoryByTypeId:@"3"];
        }else if ([self.strTypeName isEqualToString:@"JiNanGouWu"]) {
            strOriginal = [InterfaceHelper getHotelETCSecondLevelDirectoryByTypeId:@"4"];
        }
        self.arrLeiXing = [strOriginal componentsSeparatedByString:@","];
        rowCount = self.arrLeiXing.count;
        for (int i=0; i<self.arrLeiXing.count; i++) {
            NSLog(@"[arrLeiXing objectAtIndex:%d]=====%@",i,[arrLeiXing objectAtIndex:i]);
        }
      }
    
    //初始化
    commonTableView = [[UITableView alloc]initWithFrame:CGRectMake(10, 85, 300, 370)];
    commonTableView.delegate = self;
    commonTableView.dataSource = self;
    commonTableView.layer.borderWidth = 1;        //为table view 设置边框
    commonTableView.layer.borderColor = [[UIColor groupTableViewBackgroundColor]CGColor];
    commonTableView.separatorColor = [UIColor groupTableViewBackgroundColor];  //设置分割线的颜色
//    [commonTableView setBackgroundColor:[UIColor blackColor]];
    [self.view addSubview:commonTableView];
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(IBAction)btnBack:(id)sender
{
    [self.view removeFromSuperview];
}
-(IBAction)btnSubmitClicked:(id)sender    //确定搜索按钮
{
    
}

#pragma -mark TableViewDelegateMethods

//组的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

//行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return rowCount;
}

//各项的高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40 ;
}

//每个单元行中的内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdetify = @"cell";
    UITableViewCell *tvCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdetify] autorelease];  
    tvCell.selectionStyle = UITableViewCellSelectionStyleGray;
    
    if ([self.strBtnName isEqualToString:@"FanWei"]) {
        /* 展示数据 */
        //开头的图片
        UIImageView *imgDian = [[UIImageView alloc]initWithFrame:CGRectMake(20, 25, 10,10)];
        [imgDian setImage:[UIImage imageNamed:@"radiobutton1.png"]];
        [tvCell addSubview:imgDian];
        
        //内容   
        UILabel *labName = [[UILabel alloc]initWithFrame:CGRectMake(40, 0, 200, 60)];
        [labName setTextAlignment:UITextAlignmentLeft];
        [labName setBackgroundColor:[UIColor clearColor]];
        [labName setFont:[UIFont systemFontOfSize:18.0f]];
        [labName setText:[arrFanWei objectAtIndex:indexPath.row]];
        [tvCell addSubview:labName];
        
    }else if ([self.strBtnName isEqualToString:@"LeiXing"]){
        /* 展示数据 */
        //开头的图片
        UIImageView *imgDian = [[UIImageView alloc]initWithFrame:CGRectMake(20, 25, 10,10)];
        [imgDian setImage:[UIImage imageNamed:@"radiobutton1.png"]];
        [tvCell addSubview:imgDian];
        
        //内容   
        UILabel *labName = [[UILabel alloc]initWithFrame:CGRectMake(40, 0, 200, 60)];
        [labName setTextAlignment:UITextAlignmentLeft];
        [labName setBackgroundColor:[UIColor clearColor]];
        [labName setFont:[UIFont systemFontOfSize:18.0f]];
        NSLog(@"2===========%d",indexPath.row);
        [labName setText:[self.arrLeiXing objectAtIndex:indexPath.row]];
        [tvCell addSubview:labName];
    }
    
    return tvCell;
}
原文地址:https://www.cnblogs.com/ygm900/p/3090985.html