【问题标题】:If statement in TableView Cell not working如果 TableView 单元格中的语句不起作用
【发布时间】:2014-02-20 23:50:22
【问题描述】:

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

主视图代码:

#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

列表:

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

【问题讨论】:

  • 我知道这对你来说可能都是新的,但只是一个善意的建议:如果你还没有,学习使用调试器和 NSLogs。使用调试器,学习如何逐行执行代码、检查变量值(在应用程序运行时)、设置断点等。这将有助于解开“不工作”的一些谜团。
  • 这段代码需要格式化:(你怎么能用所有的随机括号来阅读这个

标签: ios uitableview


【解决方案1】:

移动

 return self;

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

以后使用断点甚至可以查看代码是否被执行。

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

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

- (void) awakeFromNib

【讨论】:

  • 你的意思是不工作?你调试了吗?放断点,打印值。这是非常简单的代码,如果你运行它应该很容易找到问题。你如何创建 checkboxButton ? Xib 还是以编程方式?也许它是零。
  • 我有一个 NS 日志来显示 'checked' 的值,但日志中没有显示任何内容。几乎就像没有看到或执行该功能一样。
  • 如何创建单元格 - 使用表视图委托更新代码
  • 复选框按钮在 IB 中设置,作为表格单元格的一部分。
  • 顺便说一句,为什么你的逻辑中有 checkboxSelected 变量而不是检查按钮 isSelected ?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-26
  • 2013-06-20
  • 2013-09-04
  • 2019-09-02
  • 2013-09-17
  • 2012-10-18
相关资源
最近更新 更多