这是我的cellForRowAtIndex
- (UItableVIEwCell *)tableVIEw:(UItableVIEw *)tableVIEw cellForRowAtIndexPath:(NSIndexPath *)indexPath { UItableVIEwCell *cell = nil; static Nsstring *MyIDentifIEr = @"GenericCell"; cell = [tableVIEw dequeueReusableCellWithIDentifIEr:MyIDentifIEr]; if (cell == nil) { cell = [[UItableVIEwCell alloc] initWithStyle:UItableVIEwCellStyleDefault reuseIDentifIEr:MyIDentifIEr] ; } Nsstring *text = nil; if(indexPath.section == CREDENTIALS_SECTION){ if (indexPath.row == 0) { NSLog(@"tot hIEr login"); UITextFIEld *login = [[UITextFIEld alloc] initWithFrame:CGRectMake(110,10,185,30)]; login.adjustsFontSizetoFitWIDth = YES; login.placeholder = @"example@gmail.com"; login.keyboardType = UIKeyboardTypeEmailAddress; login.returnKeyType = UIReturnKeyNext; login.backgroundcolor = [UIcolor clearcolor]; login.tag = 0; login.delegate = self; [login setEnabled: YES]; [cell addSubvIEw:login]; }else if (indexPath.row == 1){ NSLog(@"tot hIEr pass"); UITextFIEld *pass = [[UITextFIEld alloc] initWithFrame:CGRectMake(110,30)]; pass.adjustsFontSizetoFitWIDth = YES; pass.placeholder = @"required"; pass.keyboardType = UIKeyboardTypeDefault; pass.returnKeyType = UIReturnKeyDone; pass.secureTextEntry = YES; pass.backgroundcolor = [UIcolor clearcolor]; pass.tag = 0; pass.delegate = self; [cell addSubvIEw:pass]; } if (indexPath.row == 0) { // Email text = @"Email"; } else if(indexPath.row == 1) { text = @"Password"; } }else if(indexPath.section == METHODS_SECTION){ UISwitch *toggleSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(220,100,30)]; toggleSwitch.tag = indexPath.row; [toggleSwitch addTarget:self action:@selector(toggleSwitched:) forControlEvents:UIControlEventValueChanged]; [cell addSubvIEw:toggleSwitch]; if (indexPath.row == 0) { // Web text = @"Web applicatIE"; } else if(indexPath.row == 1) { //Mobile text = @"MobIEle applicatIE"; } else if(indexPath.row == 2) { //Mail text = @"E-mail"; } }else if(indexPath.section == PHONE_SECTION){ UITextFIEld *phoneText = [[UITextFIEld alloc] initWithFrame:CGRectMake(20,30)]; phoneText.adjustsFontSizetoFitWIDth = YES; phoneText.Font = [UIFont FontWithname:@"Arial-BoldMT" size:18]; phoneText.keyboardType = UIKeyboardTypeNumberPad; phoneText.delegate = self; phoneText.textcolor = [UIcolor blackcolor]; phoneText.text = _person.phone; [cell addSubvIEw:phoneText]; }else if(indexPath.section == REMARK_SECTION){ UITextVIEw *textVIEw = [[UITextVIEw alloc]initWithFrame:CGRectMake(20,280,260)]; textVIEw.text = _person.remark; textVIEw.delegate = self; textVIEw.Font = [UIFont FontWithname:@"Arial" size:15.0]; textVIEw.backgroundcolor = [UIcolor clearcolor]; [cell addSubvIEw:textVIEw]; text = @""; }else if(indexPath.section == button_SECTION){ cell.backgroundcolor = [UIcolor redcolor]; text = @"test"; } cell.textLabel.text = text; return cell;}
经过一番搜索,我发现有更多人遇到这个问题.问题在于这段代码.
- (UItableVIEwCell *)tableVIEw:(UItableVIEw *)tableVIEw cellForRowAtIndexPath:(NSIndexPath *)indexPath { UItableVIEwCell *cell = nil; static Nsstring *MyIDentifIEr = @"GenericCell"; cell = [tableVIEw dequeueReusableCellWithIDentifIEr:MyIDentifIEr]; if (cell == nil) { cell = [[UItableVIEwCell alloc] initWithStyle:UItableVIEwCellStyleDefault reuseIDentifIEr:MyIDentifIEr] ; } Nsstring *text = nil;
但我没有找到解决方案.
希望有人可以帮忙!
亲切的问候!
澄清
好的,所以你在这里看到我的表格截图.下面我有一个红色单元格(保存按钮),当我向下滚动其他单元格获得红色背景.而一些单元格,文本属性正在发生变化.
解决方法 这不是工作.显然你还没有完全理解重用机制是如何工作的.你是做什么?
首先,您获取要重复使用的单元格.如果你到目前为止得到一个 – 但问题来得更晚.如果你没有得到一个,那么你创建一个新的.
当您创建一个新的,在用户开始滚动之前就是开始的情况,然后根据节和行添加一些UIItems.我将解释为什么这实际上并不是一件好事.
然后用户滚动.细胞将从屏幕上消失,然后可以重复使用.然后,您将获取单元格以供重复使用.但很可能这些单元格已经有了额外的UI项目,因为你之前已经以这种方式使用过它们.在以下过程中,您将添加新的UI项目,无论该单元格上是否已有其他UI项目.
你能做什么:
>创建自己的自定义表格单元子类.您可能需要的每组附加UI项目的一个子类.这可能是最好的方式.对于每个子类,使用不同的重用标识符(!!!)这是我推荐的!但是,还有其他选择:>您仍然可以使用您的概念,但为每种类型的单元格创建单独类型的重用标识符,其中包含某些类型的附加UI项目.如果是这样,那么请确保仅在代码的if(cell == nil)分支中创建这些UI项并将其添加为子视图.只创建一次然后重复使用它们.单元重用-IDS可以是“电子邮件显示”,“电子邮件输入”,“密码显示”,“密码输入”,“切换”,……>上面的解决方案的方差是,计算行和部分进入重用标识符.例如0和0的“cell-ID-0.2”第2行 – 左右.但是你仍然必须确保你真的重复使用其他UI视图,不要每次都重新创建它们当细胞充满数据时.此外,第一部分的布局取决于您是要输入密码和电子邮件还是只显示它们.您仍然需要处理这些变化.>如果cell == nil – 意味着是否重新使用了一个单元格 – 那么首先从您之前添加的每个UI项目中清除它.你可以通过标记你的UIVIEws来实现这一点 – 比如99 – (任何不同于0应该做的事情)在创建时以及重用枚举所有子视图并删除那些具有标记99的那些.尽管你可以坚持使用代码你已经做了.
总结以上是内存溢出为你收集整理的objective-c – tableviews单元格在向下滚动后会发生变化全部内容,希望文章能够帮你解决objective-c – tableviews单元格在向下滚动后会发生变化所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)