Nice programing

indexPath로 uitableViewCell을 어떻게 얻을 수 있습니까?

nicepro 2020. 11. 6. 20:01
반응형

indexPath로 uitableViewCell을 어떻게 얻을 수 있습니까?


어떻게 얻을 수 UITableViewCellindexPath? 이렇게 :

나는를 사용하여 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];

Swift 용

let indexpath = NSIndexPath(forRow: 2, inSection: 0)
let currentCell = tblTest.cellForRowAtIndexPath(indexpath) as! CellTest

Swift 4 용 (collectionview 용)

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)

I'm not quite sure what your problem is, but you need to specify the parameter name like so.

-(void) changeCellText:(NSIndexPath *) nowIndex{
    UILabel *content = (UILabel *)[[(UITableViewCell *)[(UITableView *)self cellForRowAtIndexPath:nowIndex] contentView] viewWithTag:contentTag];
    content.text = [formatter stringFromDate:checkInDate.date];
}

You can use the following code to get last cell.

UITableViewCell *cell = [tableView cellForRowAtIndexPath:lastIndexPath];

Swift 3

let indexpath = NSIndexPath(row: 0, section: 0)
let currentCell = tableView.cellForRow(at: indexpath as IndexPath)!

Swift

let indexpath = IndexPath(row: 0, section: 0)
if let cell = tableView.cellForRow(at: indexPath) as? <UITableViewCell or CustomCell> {
    cell.backgroundColor = UIColor.red
}

참고URL : https://stackoverflow.com/questions/2384435/how-can-i-get-a-uitableviewcell-by-indexpath

반응형