在静态UITableView的UITableViewCell中以编程方式添加控件

编程入门 行业动态 更新时间:2024-10-26 06:30:54
本文介绍了在静态UITableView的UITableViewCell中以编程方式添加控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个静态UITableView,我要通过cellForRowAtIndexPath方法通过NSDictionary向其填充.

I have a static UITableView to which I am populating through NSDictionary in cellForRowAtIndexPath method.

NSString *key = [sectionKeys objectAtIndex:[indexPath section]]; NSArray *contents = [sectionContents objectForKey:key]; NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]]; static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } [[cell textLabel] setText:contentForThisRow];

认为效果很好,我还需要向tableviewcells添加控件,并且每个单元格都有不同的控件.当我以编程方式将UITextField添加到cell1时,运行后的视图可以精细到5个部分,但是当我向下滚动tableview时,第6部分的单元格也将获得一个UITextField控件. UIDatePicker发生的情况也一样,我的表视图的前5个部分都很好,但是在滚动表视图时,后两个部分也获得了UIDatePicker控件.

Thinks working well, I also need to add controls to tableviewcells and every cell has different controls. When I programmatically add UITextField to cell1 the view after running is fine till 5 sections, but when I scroll my tableview further down, the cell of section 6 also gets a UITextField control. Same is happening with UIDatePicker, that first 5 sections of my tableview are fine but on scrolling the tableview the last two sections also get the UIDatePicker control.

注意:我在表视图中有九个部分,每个部分都有一个单元格,但前两个部分包含两个单元格.

Note: I have nine sections in table view each having one cell with the exception of first two sections that contain two cells.

推荐答案

我的理解:

您的单元格布局变得混乱,一个单元格的控件转到了另一个单元格.

The layout of your cells is getting messed up and controls from one cell go to the other one.

我的建议:

这是一个普遍的问题,我前一段时间遇到过,这非常令人沮丧.搜寻了几个小时后,我找到了一个解决方案:对具有不同控件的单元格使用不同的单元格标识符.

It is a common problem, I faced it a while ago and it was extremely frustrating. After a few hours of googling I found a solution: Use different cell identifiers for cells with different controls.

示例:

5个单元格:2个单元格只有一个UITextField,1个单元格具有UITextField和一个UILabel,还有2个单元格具有UIImageView.使用3个不同的单元格标识符.对于只有UITextField的单元格为1,对于具有UIImageView的单元格为1,对于具有UITextField和UILabel的单元格为1.

5 cells: 2 cells with only a UITextField, 1 cell with UITextField and a UILabel and 2 cells with UIImageView. Use 3 different cell identifiers. 1 for the cells with only a UITextField, 1 for the cells with UIImageView and 1 for the cell with UITextField and a UILabel.

希望有帮助.

更多推荐

在静态UITableView的UITableViewCell中以编程方式添加控件

本文发布于:2023-11-12 18:44:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582245.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   中以   静态   方式   UITableViewCell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!