Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 在表视图单元格中添加值_Iphone_Objective C_Ios5_Uitableview - Fatal编程技术网

Iphone 在表视图单元格中添加值

Iphone 在表视图单元格中添加值,iphone,objective-c,ios5,uitableview,Iphone,Objective C,Ios5,Uitableview,我有两个视图控制器。CardWallet视图控制器是我的表视图。然后,AddCard视图控制器是我为名为Card的对象的新实例输入值的地方。到目前为止,我正在使用委托将这些卡实例添加到名为myWallet的数组中,该数组位于我的CardWallet视图控制器中,并且可以正常工作 我想要的是,单击我的AddCard视图控制器中的按钮后,我的卡钱包视图中将显示一个新的表单元格,其名称取决于最近添加的卡实例。下面是我的代码,请检查为什么当我添加完一个新的卡实例后,我的表中没有显示任何内容。我做了一些研

我有两个视图控制器。CardWallet视图控制器是我的表视图。然后,AddCard视图控制器是我为名为Card的对象的新实例输入值的地方。到目前为止,我正在使用委托将这些卡实例添加到名为myWallet的数组中,该数组位于我的CardWallet视图控制器中,并且可以正常工作

我想要的是,单击我的AddCard视图控制器中的按钮后,我的卡钱包视图中将显示一个新的表单元格,其名称取决于最近添加的卡实例。下面是我的代码,请检查为什么当我添加完一个新的卡实例后,我的表中没有显示任何内容。我做了一些研究,并通过了一些教程,这是一个很好的,它帮助了我很多关于表视图控制器。然而,本教程并没有满足我的主要关注,因为它的表的值只来自具有静态值的数组

谢谢

CardWalletViewController.h

#import <UIKit/UIKit.h>

@interface CardWalletViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {

}

@property (nonatomic, strong) NSMutableArray *myWallet;

-(void) printArrayContents;
@end
#import <UIKit/UIKit.h>
#import "Card.h"

@class AddCardViewController;

@protocol AddCardDelegate <NSObject>

- (void)addCardViewController:(AddCardViewController *)sender
                didCreateCard:(Card *) newCard;

@end


@interface AddCardViewController : UIViewController <UITextFieldDelegate>


@property (strong, nonatomic) IBOutlet UITextField *cardNameTextField;
@property (strong, nonatomic) IBOutlet UITextField *pinTextField;
@property (strong, nonatomic) IBOutlet UITextField *pointsTextField;

@property (nonatomic, strong) id <AddCardDelegate> delegate;

@end
卡片

#import <Foundation/Foundation.h>

@interface Card : NSObject

    @property (nonatomic, strong) NSString *name;
    @property (nonatomic, strong) NSString *pin;
    @property (nonatomic) int points;

@end

在任何人开始深入研究这个问题之前,我应该先解决这个明显的问题——在添加新卡后,您是否有重新加载数据的机制(例如,从
CardWalletViewController
调用[
tableView reloadData
)?我没有看到类似的东西,每当我向表中添加新内容时,我总是使用这个*

*如果表包含太多数据,则可能只需要重新加载其中的一部分

更新1:类继承 每个目标C类都必须从层次结构中的其他类继承。默认情况下,除非您另有说明,否则所有自定义类都将从NSObject继承,NSObject是最通用的对象(如果您已经完成Java编程,则相当于object)。更改父类只需更改接口声明中
后面的类即可。所以当你说
@接口卡WalletViewController:UIViewController

您的意思是“声明一个
CardWallerViewController
自定义类,该类继承自
UIViewController
,并实现
UITableViewDelegate
UITableViewDataSource
协议”(如果您不知道协议是什么,请询问)

现在,回到你的问题上来。现在更改父类应该很容易-您只需将
:UIViewController
更改为
:UITableViewController
,就完成了。执行此操作后,您的CardWallService控制器(也是“Waller”,真的吗?)的行为将类似于
UITableView
,而不是一般的
UIView
。执行此操作时,您也不需要告诉它实现委托和数据源协议-
UITableViewController
默认情况下执行此操作

最后,当您向Xcode项目添加新文件时,您可以告诉程序您要从哪个类继承。对于视图,它默认为UIView,但这只是因为这是最通用的视图类。当您开始使用更具体的类时(
UITableViewController
UIPickerViewController
UITableViewCell
,仅举几个例子),在bat之外更改父类将证明非常有用

更新2:UITableViewCells 对于你已经进行的循环,有很多(相对)的工作你不需要做。由于您的表直接对应于myWallet属性,这意味着表的第N行中的单元格将表示数组索引N处的卡。你可以利用这个优势。在
tableView:cellforrowatinexpath:
方法中,告诉程序在特定的indexPath(实际上只是该表的节+行)处如何处理单元格。诀窍是,这个方法一次实例化一个单元格。因此,您可以说(在末尾)
cell.textlab.text=[self.myWallet objectAtIndex:indextPath.row].name


对于第N行中的任何单元格,这将查看
myWallet
中的第N个卡片对象,并使用其名称设置单元格的
textlab.text
。如果出现问题,请将[self.myWallet objectAtIndex:indextPath.row]
保存在某个tempCard对象中,然后执行
cell.textlab.text=tempCard.name
。这也是在tableView中填充单元格的正确方法-您一次只关心一个单元格,因为该方法就是这样工作的。想象一下,如果阵列中有1000000张卡,执行for循环将迫使程序对每个单元遍历阵列1000000次。向10000000000操作问好:)

我想你可以将imageview作为子视图添加到单元格中

      UIImageView *imageView = [[UIImageView alloc] initWithFrame:cell.frame]; 

UIImage *image = [UIImage imageNamed:@"LightGrey.png"];

imageView.image = image;


 [cell addSubview:imageView];  

[[cell textLabel] setBackgroundColor:[UIColor clearColor]];

[[cell detailTextLabel] setBackgroundColor:[UIColor clearColor]];

我没有用过类似的东西,你能讨论一下它是如何完成的吗?我会在你的
addCardViewController:didCreateCard
末尾添加[tableView reloadData]。我不是专家,但按照我的理解,告诉表重新加载其数据实际上就是重新加载所有数据。因此,在调整行数时,它会为表中的每个单元格调用
tableView:cellforrowatinexpath:
。您建议将[tableView reloadData]放在这个方法中吗?-(void)addCardViewController:(addCardViewController*)发送方didCreateCard:(Card*)新卡{//将新卡插入数组[self.myWallet addObject:newCard];[self printArrayContent];}是。按照现在的方式,您向myWallet数组添加了一个新对象,但从未告诉表您添加了。因此,因为它是懒惰的,所以它从不更新表。我想更好的方法是使用
[tableView BeginUpdate]
[tableView EndUpdate]
,但我不准备在这里给你一个好的建议,除非先做一些研究。我认为@Argent有这个权利。表需要知道行已添加。无需执行开始/结束更新,因为在调用委托方法时,tableview是隐藏的。雷洛
#import <Foundation/Foundation.h>

@interface Card : NSObject

    @property (nonatomic, strong) NSString *name;
    @property (nonatomic, strong) NSString *pin;
    @property (nonatomic) int points;

@end
#import "Card.h"

@implementation Card

@synthesize name = _name;
@synthesize pin = _pin;
@synthesize points = _points;

@end
      UIImageView *imageView = [[UIImageView alloc] initWithFrame:cell.frame]; 

UIImage *image = [UIImage imageNamed:@"LightGrey.png"];

imageView.image = image;


 [cell addSubview:imageView];  

[[cell textLabel] setBackgroundColor:[UIColor clearColor]];

[[cell detailTextLabel] setBackgroundColor:[UIColor clearColor]];