2012-03-13 74 views
1

我工作的一个应用程序,我有填充了该方法的tableView一个NSMutableArray一个UITableView::,的cellForRowAtIndexPath它工作正常,但问题是,我想改变形象为tableViewCell,当我尝试访问单元格时,它总是返回null。我在这里给的代码,请告诉我,我是否失去了一些东西......的cellForRowAtIndexPath返回空值

在viewController.h文件

@interface DevicesViewController : UIViewController{ 
    IBOutlet UITableView *deviceTableVIew; 

    NSMutableArray *devicesArray; 
    NSMutableArray *deviceDetailArray; 
} 

@property (nonatomic,retain) IBOutlet UITableView *deviceTableVIew; 
@property (nonatomic,retain) NSMutableArray *devicesArray; 
@property (nonatomic,retain) NSMutableArray *deviceDetailArray; 

-(IBAction)setDevicesOn:(id)sender; 
-(IBAction)setDevicesOff:(id)sender; 

@end 

视图Controller.m或者文件

-(IBAction)setDevicesOn:(id)sender{ 

    UITableViewCell *cell = [deviceTableVIew cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:1]]; 

    cell.imageView.image = [UIImage imageNamed:@"device-on-image.png"]; 

    [deviceTableVIew reloadData]; 

    ... 
} 

-(IBAction)setDevicesOff:(id)sender{ 

    UITableViewCell *cell = [deviceTableVIew cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]]; 

    cell.imageView.image = [UIImage imageNamed:@"device-off-image.png"]; 

    [deviceTableVIew reloadData]; 

    ... 

} 

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
     return 1; 
    } 

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
     return [devicesArray 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]; 
     } 

     cell.textLabel.text = [devicesArray objectAtIndex:indexPath.row]; 
     cell.detailTextLabel.text = [deviceDetailArray objectAtIndex:indexPath.row]; 
     cell.imageView.image = [UIImage imageNamed:@"device-off-image.png"]; 

     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

     return cell; 
    } 
+0

向你展示的cellForRowAtIndexPath – janusbalatbat 2012-03-13 12:31:52

回答

5

你的UITableViewDataSource(你的viewController)告诉tableView它只有一个节。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

setDevicesOff:方法使用的是与indexPath 1.
一个部分由于第一部分具有0 indexPath第1名试图的索引引用所述第二部分在你的tableView。你的tableView没有该部分,并返回零,因为这一点。

试试这个:

-(IBAction)setDevicesOff:(id)sender{ 

    UITableViewCell *cell = [deviceTableVIew cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]; 
    cell.imageView.image = [UIImage imageNamed:@"device-off-image.png"]; 
    //[deviceTableVIew reloadData]; this shouldn't be necessary 
    ... 
} 
+0

感谢的人的代码。有效 – Hassam 2012-03-13 13:59:06

0

我认为你还没有将你的viewController声明为tableview委托和tableview数据源。

@interface DevicesViewController : UIViewController<UITableViewDataSource,UITableViewDelegate> 

我无法看到的.m文件,但我认为你已经设置委托和数据源分配内存之后的表视图。请检查所有这些东西,代码将正常工作。

希望这会帮助你。