问题描述
我有一个静态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中以编程方式添加控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!