cellForRowAtIndexPath 返回空值 [英] cellForRowAtIndexPath returns null value

查看:54
本文介绍了cellForRowAtIndexPath 返回空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,我有一个 UITableView,它填充了一个 NSMutableArray 和方法 tableView:cellForRowAtIndexPath:,它工作正常,但问题是我想更改 tableViewCell 的图像,当我尝试访问它始终返回 null 的单元格.我在这里提供代码,请告诉我是否遗漏了什么......

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...

在 viewController.h 文件中

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

在视图 controller.m 文件中

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;
    }

推荐答案

你的 UITableViewDataSource(你的 viewController)告诉 tableView 它只有一个部分.

Your UITableViewDataSource (your viewController) tells the tableView that it has one section only.

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

在您的 setDevicesOff: 方法中,您使用的 indexPath 部分为 1.
因为第一个部分的索引为 0,所以第 1 部分的 indexPath 尝试引用 tableView 中的第二部分.您的 tableView 没有该部分,因此返回 nil.

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.

试试这个:

-(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
    ...
}

这篇关于cellForRowAtIndexPath 返回空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆