How to fill one UItableViewCell with odd and even entries from an NSMutableArray?
I have an uitableview with UITableViewCellSTyleValue1 so i have a textlabel and a detail text label.
The second thing i've got is a NSMutableArray. Now i want to fill the textLabels with the even items and the detailTextlabels with the odd entries.
So for example cell one(zero) gets entry 0 and 1 cell one 开发者_如何学Gogets 2 and 3 and so on.
I didn't get it working until now.
That must be pretty easy to do (if you have problems only with distributing array items between corresponding cell labels)... As every cell "consumes" 2 array entry - number of cells equals [dataArray count]/2:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [dataArray count]/2;
}
Then in your cellForRowAtIndexPath implementation:
...
cell.textLabel.text = [dataArray objectAtIndex:2*indexPath.row];
cell.detailTextLabel.text = [dataArray objectAtIndex:2*indexPath.row + 1];
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// the usual cell creation/reuse stuff..
// obtain Label1, 2 by viewWithTag..
// index is your instance variable..
index = indexPath.row * 2;
Label1.text = [myArr objectAtIndex:index];
Label2.text = [myArr objectAtIndex:index + 1];
}
精彩评论