cellForRowAtIndexPath возвращает нулевое значение - PullRequest
1 голос
/ 13 марта 2012

Я работаю над приложением, у меня есть UITableView, который заполняется NSMutableArray с методом tableView: cellForRowAtIndexPath :, он работает нормально, но проблема в том, что я хочу изменить изображение для tableViewCell, когда я пытаюсь доступ к ячейке всегда возвращает ноль. Я даю код здесь, пожалуйста, скажите мне, я что-то упустил ...

в файле 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

в файле view 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;
    }

Ответы [ 2 ]

5 голосов
/ 13 марта 2012

Ваш 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 голосов
/ 13 марта 2012

я думаю, что вы не объявили свой viewController как источник данных tableview делегата и tableview.

@interface DevicesViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

Я не смог увидеть файл .m, но я думаю, что вы установили делегат и источник данных для табличного представления после выделения памяти для него. Пожалуйста, проверьте все эти вещи, и код будет работать нормально.

надеюсь, это поможет вам.

...