liv a
liv a

Reputation: 3340

Adding UITableViewCell programmatically to UITableView

I have created a custom UITableViewCell programmatically and wish to add it to a UITableView of UITableViewController created in IB. How can I do that?

Upvotes: 3

Views: 5962

Answers (2)

iSmita
iSmita

Reputation: 1292

Try this -

In cellForRowAtIndexPath method add your custom cell like this -

static NSString *CellIdentifier = @"DashboardCell";

DashboardTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    cell = [[DashboardTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

If cell contains image,text,etc then add like this -

cell.image.image = [UIImage imageNamed:[apdelobj.array1_25 objectAtIndex:myInt]];

cell.titleLabel.text = dashboardList.mistohMessage;

Upvotes: 2

Valeriy Van
Valeriy Van

Reputation: 1865

UITableViewDataSource's delegate method -(UITableViewCell*)tableView:(UITableView*)t cellForRowAtIndexPath:(NSIndexPath*)indexPath is just for that, no matter how cell was created, programmatically or in IB. Just return your created cell from this method. Don't forget to populate created cell with your data.

So you have provide UITableViewDataSource methods. Except mentioned above, this delegate has one more required method, -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section. Everything else is not necessary, but often useful. Consult UITableViewDataSource protocol reference.

Upvotes: 0

Related Questions