UITableViewCell with image on the right?

前端 未结 11 1078
南方客
南方客 2020-12-13 05:48

Is there a way to set the UITableViewCell.image to display on the right hand side of the cell instead of the left? Or will I need to add a separate UIImageView on the right

相关标签:
11条回答
  • This Soln is for adding different image in each cell....Just a Try

    // Customize the appearance of table view cells.
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    if(indexPath.row == 0) {
        cell.image = [UIImage imageNamed:@"image.png"];
    }
    
    else if (indexPath.row == 1) {
        cell.image = [UIImage imageNamed:@"image1.png"];
    }
    
    0 讨论(0)
  • 2020-12-13 06:38

    in swift3 and swift4, we can use this:

    cell.accessoryView = UIImageView(image:UIImage(named:"imageNmae")!)
    
    0 讨论(0)
  • 2020-12-13 06:41

    You can resize image in the accessoryview by calling setFrame method of imageview like this: [imageView setFrame:CGRectMake(0, 0, 35.0, 35.0)];

    0 讨论(0)
  • 2020-12-13 06:44

    There is a way to set the position programmatically. Here is the Swift version of the solution:

    image.frame = CGRect.init(
    x: self.view.frame.width - image.frame.width*1.1,
    y: image.frame.origin.y,
    width: image.frame.width,
    height: image.frame.height)
    

    This will position your image on the right side of the cell. This solution is adjusting automatically the position according to screen size. The only down side is when you rotate the device you need to reload your data, but you can just override in your UITableView class the didRotate listener method:

    override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
        _tableView.reloadData()}
    
    0 讨论(0)
  • 2020-12-13 06:45

    To Add image @ Right Hand side in the Cell, I would suggest you to create a Custom cell with imageview on right hand side , it will be very useful for you . and add an empty view to link this nib with customcell class.

    Now in your cell's nib file remove the view,add tableViewcell and in that cell drag & drop an imageView to right hand side.

    Now go to the TableViewCell's class say DemoCell , create an outlet and set it with the imageview in the tablecell's nib

    Now,in tableview's cellforrowatindexpath method reference your cell and use it with the nib name like this

    static NSString *CellIdentifier = @"Cell";
    DemoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[SettingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell = [[[NSBundle mainBundle]loadNibNamed:@"DemoCell" owner:self options:nil] lastObject];
    }
    

    and set the image as per your requirement

    cell.imageVw.img ....

    0 讨论(0)
提交回复
热议问题