2012-03-13 75 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文件,但我認爲你已經設置委託和數據源分配內存之後的表視圖。請檢查所有這些東西,代碼將正常工作。

希望這會幫助你。