如果TableView Cell中的语句不起作用 [英] If statement in TableView Cell not working

查看:73
本文介绍了如果TableView Cell中的语句不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从plist(复制到文档)调用数据,该数据具有0或1值,具体取决于我是否要选择或不选择复选框。 cell.m文件中的代码如下所示,但我似乎无法改变是否选中了复选框。

I am calling data from a plist (copied to documents) which has a 0 or 1 value depending on whether I want a tickbox selected or not selected. The code in the cell.m file is as follows but I can't seem to get it to alter whether the tickbox is selected or not.

主视图代码:

#import "ffguideViewController.h"
#import "booksCell.h"


@interface ffguideViewController ()

@end

@implementation ffguideViewController

{
    NSArray *title;
    NSArray *thumbnails;
    NSArray *price;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Find out the path of books_star.plist

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory =  [paths objectAtIndex:0];
    NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"books_star.plist"];

    // Load the file content and read the data into arrays
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
    title = [dict objectForKey:@"title"];
    thumbnails = [dict objectForKey:@"thumbnail"];
    price = [dict objectForKey:@"price"];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [title count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *books_starTableIdentifier = @"booksCell";

    booksCell *cell = (booksCell *)[tableView dequeueReusableCellWithIdentifier:books_starTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"booksCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    cell.titleLabel.text = [title objectAtIndex:indexPath.row];
    cell.thumbnailImageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
    cell.priceLabel.text = [price objectAtIndex:indexPath.row];

    return cell;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 104;
}

@end

单位代码:

#import "booksCell.h"

@implementation booksCell

@synthesize titleLabel = _titleLabel;
@synthesize priceLabel = _priceLabel;
@synthesize thumbnailImageView = _thumbnailImageView;


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {}
return self;}


- (void) awakeFromNib {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory =  [paths objectAtIndex:0];
    NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"books_star.plist"];

    // Load the file content and read the data into arrays
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];

    NSString *checked = [dict objectForKey:@"check"];

    NSLog(@"%@", [[dict objectForKey:@"check"] class]);

    NSLog(@"Checked %@", checked);

    int checkedINT = [checked intValue];

     NSLog(@"CheckedInt %d", checkedINT);

    if (checkedINT == 1){
        checkedImage.image = [UIImage imageNamed:@"check_on.png"];
    } else {
        checkedImage.image = [UIImage imageNamed:@"check_off.png"];}}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


@end

plist:

    <key>title</key>
    <array>
        <string>Book 1</string>
        <string>Book 2</string>
        <string>Book 3</string>
        <string>Book 4</string>
        <string>Book 5</string>
        <string>Book 6</string>
    </array>
    <key>thumbnail</key>
    <array>
        <string>test.jpg</string>
        <string>test.jpg</string>
        <string>test.jpg</string>
        <string>testjpg</string>
        <string>test.jpg</string>
        <string>test.jpg</string>
        <string>test.jpg</string>
    </array>
    <key>price</key>
    <array>
        <string>£1.00</string>
        <string>£2.00</string>
        <string>£3.00</string>
        <string>£4.00</string>
        <string>£5.00</string>
        <string>£6.00</string>
        <string>£7.00</string>
    </array>
    <key>check</key>
    <array>
        <string>1</string>
        <string>0</string>
        <string>0</string>
        <string>1</string>
        <string>0</string>
        <string>1</string>
        <string>0</string>
    </array>
</dict>
</plist>


推荐答案

移动

 return self;

到代码运行之前的函数结束。

to the end of function not before your code runs.

将来使用断点来查看代码是否被执行。

Use breakpoints in future to even see if code is executed.

关于我们讨论的第二部分问题:

Regarding the second part of question coming out of our discussion:

您正在从XIB(loadNibNamed)创建UITableViewCell。在这种情况下,不使用initWithStyle:而不是实现您的自定义逻辑:

You are creating a UITableViewCell from XIB (loadNibNamed). In this case initWithStyle: is not used. Instead of that implement your custom logic in:

- (void) awakeFromNib

这篇关于如果TableView Cell中的语句不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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