开发者

Change UITableView Cell Image using IF Statement?

开发者 https://www.devze.com 2022-12-27 14:32 出处:网络
I want to add an image using the cell.imageView setImage: 开发者_JS百科method. However, depending on fieldfrom an array (in the form of an NSString and called using [dog types]) I need the image at th

I want to add an image using the cell.imageView setImage: 开发者_JS百科method. However, depending on field from an array (in the form of an NSString and called using [dog types]) I need the image at the left-hand side of the cell to change to an image which reflects the type of dog.

How do I go about doing this? Thanks.


As you know for fill the cells contents you have to use the delegate method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

so, you have to put your IF inside this method and use indexPath.row to understand witch row you are drawing. Here a simple example:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"] autorelease];
    }
    UIImage *a,*b; //Remeber to init with your images:)
    if(indexPath.row == 1)
    {
        [cell.imageView setImage:a];
    }
    else
    {
        [cell.imageView setImage:b];
    }
    return cell;
}

I hope it's what you was looking for :)


I think you can deffinatly can do wht you want to do.

By chacking condition in cellForRowAtIndexPath method :

if(indexPath.row == 0)  // for first cell of tableView

if(indexPath.row == 1)   // for second cell of tableView

hope you can understand...

even if any problem leave a comment...and mark as correct if you got resolved...

0

精彩评论

暂无评论...
验证码 换一张
取 消