Reputation: 253
I am working on an application, I have a UITableView which is populated with an NSMutableArray with the method tableView: cellForRowAtIndexPath:, it works fine but the problem is that i want to change the image for the tableViewCell, when i try to access the cell it returns null always. I am giving the code here, please tell me whether I am missing something...
in the viewController.h file
@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
in the view controller.m file
-(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;
}
Upvotes: 1
Views: 4214
Reputation: 90117
Your UITableViewDataSource (your viewController) tells the tableView that it has one section only.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
in your setDevicesOff:
method you are using a indexPath with a section of 1.
Because the first section has an index of 0 the indexPath with section 1 tries to reference the second section in your tableView. Your tableView doesn't have that section, and returns nil because of that.
try this:
-(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
...
}
Upvotes: 5
Reputation: 493
i think You haven't declared your viewController as tableview delegate and tableview datasource.
@interface DevicesViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
I couldn't see the .m file but i think you have set delegate and datasource for the table view after allocating memory to it. Please check all these things and code will work fine.
hope this will help you.
Upvotes: 0