【发布时间】:2014-04-23 07:43:23
【问题描述】:
我是 iOS 编程新手,我想创建一个可扩展的表格单元格,里面有一个文本字段。我必须唯一地保存在每个单元格中输入的文本,但是当我在文本字段中输入一些文本时移动到另一个单元格,文本被删除。我也在这里发布了我的代码。
帮我解决这个问题 提前致谢
viewcontroller.h
#import <UIKit/UIKit.h>
@interface CNViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>{
int selectedIndex;
NSMutableArray *title;
UITextField *comments;
}
@end
viewcontroller.m
#import "CNViewController.h"
#import "CNExapndingTableViewCell.h"
@interface CNViewController ()
@property (nonatomic,strong) IBOutlet UITableView *tableView;
@end
@implementation CNViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.tableView.delegate=self;
self.tableView.dataSource=self;
//Set Index to -1 sating no cell is expanded or should Expand
selectedIndex = -1;
title=[[NSMutableArray alloc]init];
NSString *string;
for(int ii=1;ii<=10;ii++){
string = [[NSString alloc]initWithFormat:@"Row %i ",ii];
[title addObject:string];
//NSUserDefaults *userdf = [NSUserDefaults standardUserDefaults];
//self.textView = [userdf objectForKey:@"yeon"];
}
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return title.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"expandingCell";
CNExapndingTableViewCell *cell=(CNExapndingTableViewCell *) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell==nil){
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"ExpandingCell" owner:self options:nil];
cell=[nib objectAtIndex:0];
}
if(selectedIndex==indexPath.row){
//Do Expanded cell Stuff
[cell.textView setHidden:NO];
cell.textView.delegate=self;
cell.textView.tag=indexPath.row;
}
else{
//Do Closed Cell Stuff
[cell.textView setHidden:YES];
}
cell.titleLabel.text=[title objectAtIndex:indexPath.row];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if(selectedIndex==indexPath.row){
return 120;
}else{
return 44;
}
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//User Taps Expanded row
if(selectedIndex==indexPath.row){
selectedIndex=-1;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
return;
}
//user taps on different row
if(selectedIndex!=indexPath.row){
NSIndexPath *prevPath=[NSIndexPath indexPathForRow:selectedIndex inSection:0];
selectedIndex=indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:prevPath] withRowAnimation:UITableViewRowAnimationFade];
}
//user taps on new row if none is selected
selectedIndex=indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
return;
}
@end
细胞.h
#import <UIKit/UIKit.h>
@interface CNExapndingTableViewCell : UITableViewCell{
}
@property (nonatomic,strong) IBOutlet UILabel *titleLabel;
@property (nonatomic,readwrite) IBOutlet UITextField *textView;
@end
细胞.m
#import "CNExapndingTableViewCell.h"
@implementation CNExapndingTableViewCell
@synthesize titleLabel,textView;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)awakeFromNib
{
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
【问题讨论】:
-
您需要保存文本,然后在 cellForRowAtIndexPath 中您需要将 textviews 文本设置回保存的文本。
标签: ios xcode uitableview uitextfield