2011-08-24 21 views
19

मेरे पास UITableView है जो किसी सर्वर से जानकारी प्राप्त करता है और डेटा को तालिका दृश्य में प्रकाशित करता है। प्रत्येक सेल के अंदर सर्वर से जानकारी है।प्रोग्रामेटिक रूप से टेबल व्यू सेल में छवि जोड़ें

इस उदाहरण के प्रयोजन के लिए, मान लीजिए जानकारी हम सर्वर से प्राप्त संख्या हैं: 1, 2, 3, 4

मुझे क्या करना चाहते हैं एक चित्र जोड़ने (प्रोग्राम के रूप में, क्योंकि वहाँ अगर हो रहा है बयान शामिल है, आदि) सेल के बाईं ओर, और पाठ (1, 2, आदि) इसके बगल में।

_____________________________________ 
| (IMAGE) (TEXT)      | --CELL 0 
-------------------------------------- 
_____________________________________ 
| (IMAGE) 2       | --CELL 1 
-------------------------------------- 

कृपया मेरे कच्चे चित्रण बहाना:

मूल रूप से, मैं इस तरह देखने के लिए प्रत्येक कोशिका चाहते हैं। :)

उत्तर

19
यहाँ

तुम जाओ:

cell.imageView.image = [UIImage imageNamed:@"image.png"]; 

स्विफ्ट:

cell.imageView?.image = UIImage(named: "image.png") 
+0

कि इस मुद्दे में तय किया जाना चाहिए मेरी 24 सितंबर 2015 को उत्तर का संपादन। –

3

UITableViewCell ऑब्जेक्ट्स में imageView संपत्ति है; आप बस इस छवि दृश्य की छवि को सेट कर सकते हैं और यह स्वचालित रूप से छवि को इसके आगे के शीर्षक के साथ सही स्थान पर प्रदर्शित करेगा। UITableViewCell को

कुछ "निर्मित" गुण
- (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]; 
    } 

    [[cell imageView] setImage:anImage]; 
    [[cell textLabel] setText:[NSString stringWithFormat:@"%d",[indexPath row]]; 

    return cell; 
} 

देखते हैं कि आप, इस तरह के लाभ ले सकते हैं:

8

तुम इतनी तरह अपने UITableViewController उपवर्ग में छवि और UITableViewCell के लिए पाठ जोड़ सकते हैं imageView, textLabel, और DetailTextLabel के रूप में। अधिक जानकारी के लिए, Table View Programming Guide देखें।

0
// create a rectangle box for image view 

UIImageView *iconImage = [[UIImageView alloc] init]; 
iconImage.frame = CGRectMake(12,4,53.5,53.5); 

// Create a label for cells 
UILabel *cellName = [[UILabel alloc] initWithFrame:CGRectMake(75,18,200,20)]; 
cellName.font = [self fontForBodyTextStyle]; 

NSString *cellNameStr = [NSString stringWithFormat:@"%@",self.tableCellNames[indexPath.row]]; 

// Select path row tab actions 
switch (indexPath.row) { 
    case 0: 

     cellName.text = cellNameStr; 
     iconImage.image = [UIImage imageNamed:@"condition.png"]; 
     [cell.contentView addSubview:iconImage]; 
     [cell.contentView addSubview:cellName]; 

     break; 

    case 1: 
     cellName.text = cellNameStr; 
     iconImage.image = [UIImage imageNamed:@"videos.png"]; 
     [cell.contentView addSubview:iconImage]; 
     [cell.contentView addSubview:cellName]; 
     break; 

    case 2: 
     cellName.text = cellNameStr; 
     iconImage.image = [UIImage imageNamed:@"provider.png"]; 
     [cell.contentView addSubview:iconImage]; 
     [cell.contentView addSubview:cellName]; 
     break; 

    case 3: 
     cellName.text = cellNameStr; 
     iconImage.image = [UIImage imageNamed:@"info.png"]; 
     [cell.contentView addSubview:iconImage]; 
     [cell.contentView addSubview:cellName]; 
     break; 

    default: 
     NSAssert(NO, @"Unhandled value in cellForRowAtIndexPath"); 
     break; 
} 
5

पहले उत्तर की स्विफ्ट 2.0 संस्करण है:

cell.imageView?.image = UIImage(named: "image.png") 
संबंधित मुद्दे