释放自定义类对象
我有一个名为 Res 的类,如下
@interface Res : NSObject {
int _id;
NSString *_name;
NSString *_comments;
// ... and to many other objects.
}@property (nonatomic) int id;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *comments;
在我的视图中,我有一个 UITableView,我希望用户在 UITableViewCell 内的 UITextfield 中输入值,所以我添加了以下代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//code for creating cell
switch (indexPath.row) {
case 0:
// Label
[[cell textLabel] setText:@"Name :"];
// Textbox
UITextField *_txtName = [[UITextField alloc] initWithFrame:CGRectMake(100, 0, 180, 44)];
[_txtName setDelegate:self];
[_txtName setTag:0];
[cell.contentView addSubview:_txtName];
[_txtName release], _txtName = nil;
break;
case 1:
// ......
}
方法获取它
- (void)textFieldDidEndEditing:(UITextField *)textField
{
switch ([textField tag]) {
case 0:
[_resTemp setName:[textField text]];
break;
default:
break;
}
}
现在,当用户在文本框中输入值时,我通过以下我声明的 _resTemp 变量作为我的 .h 文件中的实例变量
在 ViewDidLoad
方法上我写 _resTemp = [[Res alloc] init];
在 ViewDidUnload
方法上,我像 [_resTemp release];
一样释放它 我也在 dealloc 方法中以同样的方式释放它。
我仍然遇到有关此变量的内存泄漏。
我不知道在哪里释放这个对象,或者我是否需要改变我的逻辑。 谁能给我一些引用 UITableView 中数据输入代码的链接?
I have a class called Res as follow
@interface Res : NSObject {
int _id;
NSString *_name;
NSString *_comments;
// ... and to many other objects.
}@property (nonatomic) int id;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *comments;
On my view I have a UITableView, I want user to enter values in UITextfield inside UITableViewCell so I have added below code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//code for creating cell
switch (indexPath.row) {
case 0:
// Label
[[cell textLabel] setText:@"Name :"];
// Textbox
UITextField *_txtName = [[UITextField alloc] initWithFrame:CGRectMake(100, 0, 180, 44)];
[_txtName setDelegate:self];
[_txtName setTag:0];
[cell.contentView addSubview:_txtName];
[_txtName release], _txtName = nil;
break;
case 1:
// ......
}
Now when user enter values in textbox I get it via following method
- (void)textFieldDidEndEditing:(UITextField *)textField
{
switch ([textField tag]) {
case 0:
[_resTemp setName:[textField text]];
break;
default:
break;
}
}
I declared _resTemp
variable as instance variable in my .h file
on ViewDidLoad
method I write _resTemp = [[Res alloc] init];
and on ViewDidUnload
method I release it like [_resTemp release];
and I also release it same way in dealloc method.
Still I get memory leaks regarding this variable.
I do not get where to release this object or do I need to change my logic.
Can anyone give me some links that refers to code for data entry in UITableView?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该在 Res 类中定义一个
dealloc
方法。该方法将在释放 Res 对象之前释放它所包含的对象。
You should define a
dealloc
method in your Res class.The method will release objects contained in your Res object before releasing it.