我怎样才能通过 indexPath
获得 UITableViewCell
?像这样:
我使用 UIActionSheet
,当我点击确认时 UIButton
我想更改单元格文本。
我定义为属性的 nowIndex
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
NSInteger section = [nowIndex section];
NSInteger row = [nowIndex row];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
formatter.dateFormat = @"yyyy-MM-dd";
if (section == 0)
{
if (row == 0)
{
}
else if (row == 1)
{
UILabel *content = (UILabel *)[[(UITableView *)[actionSheet ] cellForRowAtIndexPath:nowIndex] viewWithTag:contentTag];
content.text = [formatter stringFromDate:checkInDate.date];
}
else if (row == 2)
{
}
}
}
}
[(UITableViewCell *)[(UITableView *)self cellForRowAtIndexPath:nowIndex]
会给你uitableviewcell。但我不确定你到底要什么!因为你有这段代码,但你仍然在问如何获得 uitableviewcell。更多信息将有助于回答您:)
ADD:这是一种替代语法,可以在没有强制转换的情况下实现相同的目标。
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:nowIndex];
最后,我使用以下代码获取单元格:
UITableViewCell *cell = (UITableViewCell *)[(UITableView *)self.view cellForRowAtIndexPath:nowIndex];
因为类扩展了UITableViewController:
@interface SearchHotelViewController : UITableViewController
因此,self
是“SearchHotelViewController”。
这是从索引路径获取自定义单元格的代码
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:2 inSection:0];
YourCell *cell = (YourCell *)[tblRegister cellForRowAtIndexPath:indexPath];
对于斯威夫特
let indexpath = NSIndexPath(forRow: 2, inSection: 0)
let currentCell = tblTest.cellForRowAtIndexPath(indexpath) as! CellTest
对于 Swift 4(对于集合视图)
let indexpath = NSIndexPath(row: 2, section: 0)
let cell = self.colVw!.cellForItem(at: indexpath as IndexPath) as? ColViewCell
对于斯威夫特 4
let indexPath = IndexPath(row: 0, section: 0)
let cell = tableView.cellForRow(at: indexPath)
您可以使用以下代码获取最后一个单元格。
UITableViewCell *cell = [tableView cellForRowAtIndexPath:lastIndexPath];
我不太确定您的问题是什么,但是您需要像这样指定参数名称。
-(void) changeCellText:(NSIndexPath *) nowIndex{
UILabel *content = (UILabel *)[[(UITableViewCell *)[(UITableView *)self cellForRowAtIndexPath:nowIndex] contentView] viewWithTag:contentTag];
content.text = [formatter stringFromDate:checkInDate.date];
}
斯威夫特 3
let indexpath = NSIndexPath(row: 0, section: 0)
let currentCell = tableView.cellForRow(at: indexpath as IndexPath)!
迅速
let indexpath = IndexPath(row: 0, section: 0)
if let cell = tableView.cellForRow(at: indexPath) as? <UITableViewCell or CustomCell> {
cell.backgroundColor = UIColor.red
}
dataSource
属性设置的任何地方调用它,但通常是self
。