UITableViewController以编程方式访问静态单元格
说我有一个表中有10静态单元格,有没有办法select一个特定的单元格编程?
我已经试过了
UITableViewCell *cell = [self.tableView.subviews objectAtIndex:indexPath.row];
但是这实际上并没有返回一个表格单元格。
这似乎崩溃了我的代码
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
我试图设置代码中的静态单元格的个人高度。 一个选项是为每个单独的静态单元制作网点,但这看起来很愚蠢。
要访问静态创build的单元格,请尝试以下操作:
UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
这适用于静态单元格。 所以,如果你在…
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath]; return cell; }
…委托,您可以使用上述声明访问所有静态configuration的单元格。 从那里,你可以做任何你想要的“细胞”。
我有一个ViewController,它有两个UITableViews。 其中一个有静态定义的单元格,有一个Storyboard,另一个有使用代码dynamic定义的单元格。 鉴于我使用相同的ViewController作为委托的两个表,我需要防止在cellForRowAtIndexPath被调用的位置创build新的单元格,其中已经创build了单元格。
在你的情况下,你需要获得编程访问你的单元格。
玩的开心。
创build一个@IBOutlet
。
即使您以编程方式重新排列静态单元格,这也可以工作。
你可以试试这个…
UITableViewCell *cell = (UITableViewCell*)[yourTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:rowvalue inSection:0]];
如果您需要访问单元格对象,那么使用UITableViewCell方法cellForRowAtIndexPath是相当合适的。
这可能只是通过单元格,如果它是可见的,或调用委托方法cellForRowAtIndexPath(不要混淆起来),你应该提供。 如果那个崩溃,然后深入挖掘并调查崩溃的根本原因。
使用table view delegate
方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSInteger height; if(0 == indexPath.row) { height = 44; } else { height = 50; } return height; }
这是一个Swift 2.3。 解。
UITableViewController是在IB中创build的。
/* NOTE The custom static cells must be In the IB tableview if one is being used They also must be updated to be MYCustomTableViewCell instead of UITableViewCell */ import UIKit class MYCustomTableVC: UITableViewController { override func viewDidLoad() { super.viewDidLoad() // NO Nib registration is needed } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = super.tableView(tableView, cellForRowAtIndexPath: indexPath) as! MYCustomTableViewCell // MYCustomTableViewCell can be created programmatically // without a Xib file return cell } }
- 如何避免UITableViewCell重复添加自定义标签时,TableView滚动?
- UITableView -headerViewForSection返回(null)
- iOS 9 UITableView单元格的文本标签不是UITableView的全部宽度
- iOS UITableViewCell需要按两次来调用didSelectRowAtIndexPath
- 静态和dynamic单元格的UITableView混合?
- 从UITableView保存多个值
- 长按uiTableView IOS更改单元格的select
- 如何重新创build静态单元格支持UITableViewController?
- 从ios中的Plist嵌套展开式UITableView